Attempt to index nil with 'Effect' happens sometimes

Hi! :slight_smile:
I’m trying to make an effect that appears for 1 second when someone changes Stages in a Obby.

local plr = script.Parent.Parent
local leaderstats = plr:WaitForChild("leaderstats")
local stage = leaderstats:WaitForChild("Stage")

if stage.Changed then
	plr.Character.StageConfetiEffect.Handle.ExpSwoosh.Enabled = true
	plr.Character.StageConfetiEffect.Handle.Level.ExpLines.Enabled = true
	plr.Character.StageConfetiEffect.Handle.Level.Stars.Enabled = true
	plr.Character.StageConfetiEffect.Handle.Stage.BillboardGui.Enabled = true
	plr.Character.StageConfetiEffect.Handle.Stage.BillboardGui.Frame:TweenPosition(, 0,0.2, 0),"Out","Elastic",2,true)
	plr.Character.StageConfetiEffect.Handle.Stage.BillboardGui.Frame.TextLabel.Text = "Stage "..stage.Value.."!"
	plr.Character.StageConfetiEffect.Handle.ExpSwoosh.Enabled = false
	plr.Character.StageConfetiEffect.Handle.Level.ExpLines.Enabled = false
	plr.Character.StageConfetiEffect.Handle.Level.Stars.Enabled = false
	plr.Character.StageConfetiEffect.Handle.Stage.BillboardGui.Enabled = false
	plr.Character.StageConfetiEffect.Handle.Stage.BillboardGui.Frame:TweenPosition(, 0,0.75, 0),"Out","Elastic",0,true)

Sometimes it works perfectly. Sometimes it gives this error when I press Play:

Players.ZuperManoz.PlayerScripts.StageEffect:15: attempt to index nil with 'StageConfetiEffect'

I think it happens when the StageConfetiEffect isn’t inside plr.Character when the script runs, because sometimes it works and sometimes it doesn’t…

Can someone help me?

1 Like

instead of that,
connect it to a .Changed. event , like here:


Also, use task.wait instead of wait.


Oops sorry i misunderstood the error could you send an image of the character in explorer when the error occurs?

1 Like

Ty for helping!
This worked, but the error sometimes still appears right after i hit Play!

Ty for helping!
When I go inside my character everything is already there…

@Valkyrop already fixed the issue with stage.Changed:Connect(function().
It works everytime I step on a Checkpoint, but sometimes the error still pops up when I hit Play… Do you know how to fix it??

Try this:

local plr = script.Parent.Parent
local leaderstats = plr:WaitForChild("leaderstats")
local stage = leaderstats:WaitForChild("Stage")

	local character = plr.Character or plr.CharacterAdded:Wait()
	local StageConfetiEffect = character:WaitForChild("StageConfetiEffect")
	StageConfetiEffect.Handle.ExpSwoosh.Enabled = true
	StageConfetiEffect.Handle.Level.ExpLines.Enabled = true
	StageConfetiEffect.Handle.Level.Stars.Enabled = true
	StageConfetiEffect.Handle.Stage.BillboardGui.Enabled = true
	StageConfetiEffect.Handle.Stage.BillboardGui.Frame:TweenPosition(UDim2.fromScale(0, 0.2), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 2, true)
	StageConfetiEffect.Handle.Stage.BillboardGui.Frame.TextLabel.Text = "Stage "..stage.Value.."!"
	StageConfetiEffect.Handle.ExpSwoosh.Enabled = false
	StageConfetiEffect.Handle.Level.ExpLines.Enabled = false
	StageConfetiEffect.Handle.Level.Stars.Enabled = false
	StageConfetiEffect.Handle.Stage.BillboardGui.Enabled = false
	StageConfetiEffect.Handle.Stage.BillboardGui.Frame:TweenPosition(UDim2.fromScale(0, 0.75), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 0, true)

You needed to check if the character existed and also add a :WaitForChild so it yields until the object can load in.

1 Like

Ty for helping!
This fixed all errors!

1 Like

No problem. If you have any more questions, feel free to ask. Also, goodluck on your game.

1 Like