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

Can somebody explain to me why my script isn't working with the "else" statement?

Asked by 5 years ago
Edited 5 years ago

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
0
Basically if the Districtnumber21 == nil then I want the script to skip lines 3-10 and move on to the rest of the decals and I want it to go on this way etc. Technozero3 2 — 5y
0
You are right. XX_Doggoa 24 — 5y
0
But it isn't working? Technozero3 2 — 5y

1 answer

Log in to vote
0
Answered by
Ankur_007 290 Moderation Voter
5 years ago

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 it
  • You should always define variables with the local 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 :)

0
You forgot "game.workspace". DeceptiveCaster 3761 — 5y
Ad

Answer this question