Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make an 'if' 'then' loop with two conditions?

Asked by
mrcool2 75
9 years ago

I only just started to learn how to script yesterday, and I have come across this issue, and I do not know how to do it, I've read about else and elseif but I believe they function as the opposite of what I need. Basically I'm trying to make a script that will light a player's unlit hand-torch when it comes into contact with a block that is on fire.

I have tried two methods out, both appear to work in testing however I am not sure if one is a correct way of doing it, or if they're both wrong.

function light(hit)
    if script.Parent.Fire.Enabled == true and hit.Fire.Enabled == false then
            hit.Fire.Enabled = true
            hit.Light:Play()
    end
end

script.Parent.Touched:connect(light)
function light(hit)
    if script.Parent.Fire.Enabled == true  then
        if hit.Fire.Enabled == false then
            hit.Fire.Enabled = true
            hit.Light:Play()
        end
    end
end

script.Parent.Touched:connect(light)
0
Thank you very much for that tip! I didn't know that :) mrcool2 75 — 9y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Both are correct, and they are both equivalent.

Using the first one avoids an extra level of indentation, and using the second one avoids an extremely long line. You can use whichever one you prefer.


It's a little redundant to use == true and == false.

In Lua, instead of saying if x == true then, it's idiomatic to say if x then.

Similarly, instead of if x == false then, if not x then. This is a lot shorter, and often much more readable.

Ad

Answer this question