WaitForChild cant find object even tho it exists

In my game the Script cannot find an object inside the player even tho it is there in the Explorer. I get a Infinite yield possible error.

Im trying to make a checkpoint system for multiple areas

If it helps, this is my script, the error is in line 16:

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local StageWinter = Instance.new("IntValue", leaderstats)
	StageWinter.Name = "StageWinter"
	StageWinter.Value = 1
	
	local StageDesert = Instance.new("IntValue", leaderstats)
	StageDesert.Name = "StageDesert"
	StageDesert.Value = 1
	
	player.CharacterAdded:Connect(function(char)
		wait()
		local STAGE = player:WaitForChild("checkpointData"):WaitForChild("currentStage") -- HERE is the infinite yield error
		
		if STAGE.Value == "Winter" then
			char:WaitForChild("HumanoidRootPart").CFrame = game.Workspace.CheckpointsWinter:FindFirstChild("Checkpoint"..StageWinter.Value).CFrame
		elseif STAGE.Value == "Desert" then
			char:WaitForChild("HumanoidRootPart").CFrame = game.Workspace.CheckpointsDesert:FindFirstChild("Checkpoint"..StageDesert.Value).CFrame
		end
	end)
	
end)

for i, v in pairs(game.Workspace.CheckpointsWinter:GetChildren()) do
	v.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if player then
			if tonumber(string.sub(v.Name,11,#v.Name)) > player.leaderstats.StageWinter.Value then
				player.leaderstats.StageWinter.Value = tonumber(string.sub(v.Name,11,#v.Name))
			end
		end
	end)
end

for i, v in pairs(game.Workspace.CheckpointsDesert:GetChildren()) do
	v.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if player then
			if tonumber(string.sub(v.Name,11,#v.Name)) > player.leaderstats.StageDesert.Value then
				player.leaderstats.StageDesert.Value = tonumber(string.sub(v.Name,11,#v.Name))
			end
		end
	end)
end

image

I didn’t know you could put two WaitForChild() in the same line.
Where does the checkpointData folder gets added? As I saw, it didn’t get added in this script, so maybe this script ran faster than the other one, and that’s why it’s causing an infinite yield.

1 Like

Its a localscript which is in StarterPlayerScripts

checkpointData = Instance.new("Folder",script.Parent.Parent)
checkpointData.Name = "checkpointData"

currentStage = Instance.new("StringValue",checkpointData)
currentStage.Name = "currentStage"

Im quite sure that this script here runs before the one i sent above.

If it’s a Local Script, the server cannot find it, only your Client can read the folder.
You’ll need to create the checkpointData folder on the Server (Server Script).

Ohh yea that makes sense, oops