Attempt to index nil with 'WaitForChild'

I am making a game but i cam across an error again that I don’t really know how to fix, any solutions or feedback?

script:

local Players = game:GetService("Players")
local part = script.Parent


script.Parent.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = Players:GetPlayerFromCharacter(part.Parent)
		if player:WaitForChild("leaderstats") and player.WaitForChild("MapsCompleted") then
			player.leaderstats.MapsCompleted.Value = player.leaderstats.MapsCompleted.Value + 1
			hit.Parent:SetPrimaryPartCFrame(CFrame.new(Vector3.new(-121, 1.5, -619)))
		
			print("u win lol")
		end
	end
end)

Thanks for helping out! I will be very appreciated if you helped out and I will give every comment here a heart!

Change part.Parent to hit.Parent.

1 Like

The first thing I saw was that you were trying to get the player via a part varabiel instead of the hit parameter, secondly, it would still error with taht fix cause in one of them you do .WaitForChild, try this

local Players = game:GetService("Players")
local part = script.Parent


script.Parent.Touched:connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if player and player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("MapsCompleted") then
		player.leaderstats.MapsCompleted.Value += 1
		hit.Parent:SetPrimaryPartCFrame(CFrame.new(Vector3.new(-121, 1.5, -619)))
		print("u win lol")
	end
end)
2 Likes

woah that works! Thanks for helping out you deserved to be marked as a solution and have a free heart!

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like