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)
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.