Code can't find global Variables in If Statement

Making a system that checks through all BoolValues in a Part, and then make it do stuff when the BoolValue is true. That part works, but The bottom section of the code can’t find the global variables “Shinyexplosion”, “Fexplosion”, “ShinyClone”, or “FireClone” that are located in the if statements.

if descendantShiny.Value == true then
	Sexplosion = game.Workspace.ParticleFolder.ShinyExplosion:Clone()
	Sexplosion.Parent = DescendantReplace
	Sexplosion.Enabled = true
	ShinyClone = game.Workspace.SoundFolder.ShinyGrab:Clone()
	ShinyClone.Parent = game.Workspace.SoundFolder
	ShinyClone:Play()
end
if descendantFire.Value == true then
	Fexplosion = game.Workspace.ParticleFolder.FireExplosion:Clone()
	Fexplosion.Parent = DescendantReplace
	Fexplosion.Enabled = true
	FireClone = game.Workspace.SoundFolder.FireGrab:Clone()
	FireClone.Parent = game.Workspace.SoundFolder
	FireClone:Play()
end
wait(0.5)
Sexplosion.Enabled = false
Fexplosion.Enabled = false
ShinyClone:Destroy()
wait(0.3)
FireClone:Destroy()
wait(1.2)
SClone:Destroy()

I’ve tried looking over the DevForums but I can’t find anyone with a similar problem. Any help is appreciated! The error I get is: attempt to index nil with ‘Enabled’

Hello Nexxeron :wave:

I see what you are trying to do using the if-statements. However, if the “descendantShiny” or “descendantFire” values are “false” then you never assign a value to the global variables as the scopes are never even entered, leaving these variables nil.

Maybe try to redesign the code to where you check whether the variable is nil or not.
If you have questions, let me know.

DosenSuppe2_0

1 Like

Thanks! Completely overlooked that.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.