【问题标题】:Script won't work, sound doesn't play, and brick doesn't change colour脚本不工作,声音不播放,砖不变色
【发布时间】:2019-07-26 04:24:50
【问题描述】:

我和我的朋友在 Studio 上制作恐怖游戏,突然间,我解决了这个问题。我想让窗户变成白色,播放灯光声音,然后恢复正常,等待 15/20 秒并循环播放。

我已经将声音放在脚本的子项中,但它不起作用。我已经尝试了很长时间了。

        script.Parent.BrickColor=("Institutional White")
        script.Sound:Play()
        wait(0.2)
        script.Parent.BrickColor = Dark stone grey
        wait(10)
        loop
    end

预期的“=”,得到“结束”

【问题讨论】:

  • 这是您的完整代码吗?你的预期结果是什么?您的代码在语法上是错误的。错误消息的意思是,lua 正在等待分配loop,例如loop = 123。但是缺少预期的=

标签: lua roblox


【解决方案1】:

我假设您为此使用 Roblox Studio。

您在正确的轨道上,但只有一些语法错误需要修复。为零件设置 BrickColor 时,您必须创建一个新的 BrickColor 对象。您可以使用 .new() 函数来做到这一点。

script.Parent.BrickColor = BrickColor.new("Institutional White")

接下来,我们需要修复您的 while 循环。现在,您已经声明了 end 词,但您也需要 while 声明!我假设您希望循环无限运行。在这种情况下,您可以这样做:

while (true) do
    --Write your code here
end

您需要担心的另一件事是您的资产加载。简而言之,资产只是游戏中的一个对象,就像声音、图片、零件、任何东西一样。有时,当您启动游戏时,您的代码运行速度会比资源加载速度快。在这种情况下,我们需要编写代码,使用 :WaitForChild() 函数等待资产加载。

local part = script.Parent:WaitForChild("MyPart")
local sound = script.Parent:WaitForChild("MySound")

此外,Roblox Studio 中的另一件事是声音。声音的来源或播放位置取决于声音的父元素是什么。例如,如果我将声音放在myPart 中,声音会从砖块本身播放出来,并且在你走开时会变得更安静。如果您将声音放在服务或非物理对象中,例如 Workspacescript,则声音将全局播放,并且每个人都会以相同的音量听到声音。

所以,最后,你的代码应该是这样的:

local part = script.Parent
local sound = script:WaitForChild("Sound")

while (true) do
    part.BrickColor = BrickColor.new("Institutional White")
    sound:Play()
    wait(0.2)
    part.BrickColor = BrickColor.new("Medium stone grey")
    wait(10)
end

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 2023-01-23
    • 2015-03-20
    • 2011-02-25
    • 2020-02-15
    • 2015-12-22
    相关资源
    最近更新 更多