Attempt to index nil with 'WaitForChild' Fix

Im getting an error when leaving the game that says “attempt to index nil with ‘WaitForChild’” I know the cause of it, but im not sure how to fix it.

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = plr:WaitForChild("Boosts")
	local JumpTime = leaderstats:WaitForChild("JumpTime")
	local SpeedTime = leaderstats:WaitForChild("SpeedTime")
	local CoinTime = leaderstats:WaitForChild("CoinTime")

	while true do
		if players:FindFirstChild(plr.Name) then
			task.wait(1)
			if JumpTime.Value > 0 then
				JumpTime.Value -= 1
				plr.Character:WaitForChild("Humanoid").JumpPower += 2000
			end
			if SpeedTime.Value > 0 then
				SpeedTime.Value -= 1
				plr.Character:WaitForChild("Humanoid").JumpPower += 2000
			end
			if CoinTime.Value > 0 then
				CoinTime.Value -= 1
				plr.Character:WaitForChild("Humanoid").JumpPower += 2000
			end
		else
			break
		end
	end
end)

I tried adding the if players:FindFirstChild(plr.Name) then part to stop the error but It did not change anything!

The script is part of a Potion System im trying to add to my game, nothing I’ve ever done before so the code is not great.

try this:

	local leaderstats = plr:WaitForChild("Boosts")
	local JumpTime = leaderstats:WaitForChild("JumpTime")
	local SpeedTime = leaderstats:WaitForChild("SpeedTime")
	local CoinTime = leaderstats:WaitForChild("CoinTime")

	while plr do
		-- sorry for indentation :P
			if JumpTime.Value > 0 then
				JumpTime.Value -= 1
				plr.Character:WaitForChild("Humanoid").JumpPower += 2000
			end
			if SpeedTime.Value > 0 then
				SpeedTime.Value -= 1
				plr.Character:WaitForChild("Humanoid").JumpPower += 2000
			end
			if CoinTime.Value > 0 then
				CoinTime.Value -= 1
				plr.Character:WaitForChild("Humanoid").JumpPower += 2000
			end
            task.wait(1)
	end
end)

(replaced the if check with simple while loop condition)

I can see what you were trying to do with that if-else, but you overlooked an edge case that’s got to do with task.wait(1). It’s important for the while loop but it’s placed wrong. Imagine this: At the start of an iteration, the code checks if the player exists, looks good, the player is still here. Then it starts its 1 second long slumber. Now what if the player leaves during this 1 second gap? Well the script after waking up will try to run WaitForChild on the character of a player… who doesn’t exist. The task.wait(1) makes your if check useless in that situation, which only guarantees code run immediately after: the player’s existence. Which is why the wait thing was moved to the end of the iteration. Hope this helps.

plr.Character has no reference to anything yet. You might have to check for Player.CharacterAdded event first before indexing it.

Yeah, i think the player might not have a character. You should probably use an if statement or something to see if the character actually exists. if it does, go ahead and run the code. if it doesn’t, maybe print a warning in the console.

So, what is happening is that the script is trying to access an object or property that doesn’t exist when the player leaves the game the plr becomes nil or its children don’t exist anymore, you could use PlayerRemoving event to stop any stuff still going when a player leaves, you also need to check if plr still exists before attempting to access its children.

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = plr:WaitForChild("Boosts")
	local JumpTime = leaderstats:WaitForChild("JumpTime")
	local SpeedTime = leaderstats:WaitForChild("SpeedTime")
	local CoinTime = leaderstats:WaitForChild("CoinTime")
	local isPlayerActive = true

	-- this disconnects the loop when the player leaves
	plr.AncestryChanged:Connect(function()
		if not plr:IsDescendantOf(game) then
			isPlayerActive = false
		end
	end)

	while isPlayerActive do
		task.wait(1)
		
		if JumpTime.Value > 0 then
			JumpTime.Value -= 1
			if plr.Character and plr.Character:FindFirstChild("Humanoid") then
				plr.Character.Humanoid.JumpPower += 2000
			end
		end
		
		if SpeedTime.Value > 0 then
			SpeedTime.Value -= 1
			if plr.Character and plr.Character:FindFirstChild("Humanoid") then
				plr.Character.Humanoid.WalkSpeed += 2000
			end
		end
		
		if CoinTime.Value > 0 then
			CoinTime.Value -= 1
		end
	end
end)

1 Like

This is exactly what I was trying to do but Im not skilled enough to do it lol, Thank you!

1 Like

No problem! if you need anymore help just ask.