Hi everyone,
So I am a newbie to scripting and I am just starting to get the hang of certain things. I am having a problem with one of my scripts however using the “else” statement. The problem is line 3. If the screen decal for this is ~=nil then there is not a problem and the rest of the code is fine. However, if the screen decal ==nil, I want the script to skip this part of the code and move onto the next decal and so on. I thought “else” would fix this but I guess not, if someone can help me address the error it would be a life saver. Sorry if I did wrong formating, this is my first post. When line 3 ==nill then the entire script breaks and it doesn't move onto the next decal. I don't understand why.
function onClick() local final = game.workspace.FallenFolder if final.Districtnumber21 ~= nil then final[("11")].Decal.Transparency = 0 final.Districtnumber21.District.number.TextTransparency = 0 final.Districtnumber21.District.number.TextStrokeTransparency = 0 wait(.4) final.Districtnumber21:Remove() final[("11")]:Remove() else -- If final.Districtnumber21 == nil then continue on with the code right? or am I wrong? wait(.3) --continue on with code
The problem here isn't the else statement but line 3 itself. It works right when final.Districtnumber21 ~= nil
as the line doesn't error. However, when final.Districtnumber21 == nil
, the script will error. As Districtnumber21
is not a child of final
now, a direct reference to a non-existent child will error. So, you should use the :FindFirstChild(name)
method to get the line to not error when said child == nil. When a child is not found :FindFirstChild()
will return nil and won't error. Here's the script that would do your job:
lua
local function onClick()
local final = game.workspace.FallenFolder
if final:FindFirstChild("Districtnumber21") then
final[("11")].Decal.Transparency = 0
final.Districtnumber21.District.number.TextTransparency = 0
final.Districtnumber21.District.number.TextStrokeTransparency = 0
wait(.4)
final.Districtnumber21:Destroy()
final[("11")]:Destroy()
else
-- Whatever
end
-- Whatever
end
Some other stuff I have changed and I thought would be good to tell you are:
:Remove()
is deprecated, you should use :Destroy()
over itlocal
keyword i.e: local nameOfVariable = value
; you can also use this for functions as such: local function nameOfVariable(...) end
Feel free to ask any questions you have and point out any mistakes in my answer :)