Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use multiple If's in Lua?

Tags:

lua

roblox

I'm trying to make a UFO in my ROBLOX place, and wanted to create a system that would play an audio when the UFO passes overhead. I created a part, inserted an audio into the part, then placed a script in the audio. So it looks like:

Part->Audio->Script

I plan the system to register when a Humanoid is touched, and if the part is going faster than say, 300 Studs per second, I want it to play an audio (preferably the audio would be played only for the person(s) that was touched by the part) so I wrote a script that goes as follows:

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

As you can see I'm missing the part about the humanoid being touched, but I'm not sure how to write that. I'm really new to scripting, and I don't know the proper context for these commands. Help would be greatly appreciated.

Thanks!

like image 756
Marcus Avatar asked Sep 20 '25 19:09

Marcus


1 Answers

You can use Lua's logical operators: 'and', 'or', and 'not' are the most commonly used. In your case, it sounds like you want to do something like:

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

You can also "nest" if statements:

if condition1 then
    if condition2 then
        do_something()
    end
end
like image 144
Will Avatar answered Sep 23 '25 12:09

Will