【问题标题】:How do I use multiple If's in Lua?如何在 Lua 中使用多个 If?
【发布时间】:2017-05-15 14:40:28
【问题描述】:

我正在尝试在我的 ROBLOX 场所制作 UFO,并希望创建一个系统,当 UFO 从头顶经过时播放音频。我创建了一个部件,将音频插入到部件中,然后在部件中放置了一个脚本。所以它看起来像:

部分->音频->脚本

我计划系统在触摸人形机器人时进行注册,如果部件的运行速度比说每秒 300 个螺柱快,我希望它播放音频(最好只为人播放音频(s ) 被部分触及)所以我写了一个脚本如下:

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

结束

如您所见,我错过了关于被触摸的人形机器人的部分,但我不知道该怎么写。我真的是脚本新手,我不知道这些命令的正确上下文(?)。非常感谢您的帮助。

谢谢!

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    你可以使用 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
    

    【讨论】:

    • 所以到目前为止我的脚本是:
    【解决方案2】:

    除了@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()
    

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 2022-01-20
      • 2021-02-25
      • 2021-10-30
      • 1970-01-01
      相关资源
      最近更新 更多