Lua中如何使用多个if语句?

我正在尝试在我的ROBLOX场地中制作一架UFO,并想要创建一个系统,当UFO经过头顶时会播放音频。我创建了一个部分,在部分中插入了音频,然后在部分中放置了一个脚本。所以它看起来像这样:

Part->Audio->Script

我计划该系统在触碰到Humanoid时进行注册,如果该部分的速度超过300 Studs per second,则希望它播放音频(最好仅对被该部分触碰的人播放音频),因此我编写了以下代码:

while true do
if script.parent.parent.Velocity.Magnitude>299 then
    script.Parent:play()
    wait(5)
    script.Parent:stop()
else
    wait()
end
wait()

您可以看到我缺少有关触碰到Humanoid的部分,但我不知道如何编写。我是编写脚本的新手,并且不知道这些命令的正确上下文?帮助将不胜感激。

谢谢!

点赞
用户1714429
用户1714429

你可以使用Lua的逻辑运算符:'and'、'or' 和 'not' 是最常用的。在你的情况下,似乎你想做类似这样的事情:

if (condition1) and (condition2) then
    play_sound()
else
    wait()
end

你也可以 "嵌套" if 语句:

if condition1 then
    if condition2 then
        do_something()
    end
end
2016-12-31 19:20:48
用户1471485
用户1471485

除了 @Will 的答案,你还可以使用 elseif 来检查不同的条件。

if (conditionA) then
elseif (conditionB) then
end

如果条件 A 为真,则不会检查下一个语句,因此顺序很重要。

while true do
if script.parent.parent.Velocity.Magnitude>299 then
 if(humanoidIsTouchedd()) then
    script.Parent:play()
    wait(5)
    script.Parent:stop()
 elseif (somethingElseIsTouched())
    doSomethingElse()
 else
    wait()
end
wait()
2017-01-03 08:32:46