Attempt to index nil with 'FindFirstChild' error

I have this problem with my script, the error is in line 5

local player = game.Players.PlayerAdded:Connect(function(player)
	local statsy = player:FindFirstChild("Stats")
	
script.Parent.Touched:Connect(function()
		statsy:FindFirstChild("Lost").Value = true
		statsy:FindFirstChild("Win").Value = false
	end)
end)

What i did is that i made a folder on the local player, called “Stats”. there is two “BoolValue” Values in the folder called “Lost” and “Win”, and a part in the workspace. the script is on the part. when you touch the part the" Lost" value is meant to change from false to true and the “Win” one from true to false. but this appears.

image

Help if you can or you want, the awnser is surely pretty easy to figure out but i am not so advanced at programing.

Try using WaitForChild instead.

Edit: I actually noticed you made a function inside a variable, which is actually causing the player to be nil. I just removed the local player, so that should be the main fix of your script.

Code:

game.Players.PlayerAdded:Connect(function(player)
	local statsy = player:WaitForChild("Stats")
	
	script.Parent.Touched:Connect(function()
		statsy:FindFirstChild("Lost").Value = true
		statsy:FindFirstChild("Win").Value = false
	end)
end)
1 Like

that was more easy then i though, thank you so much!

1 Like