Attempt to index nil with WaitForChild

Hello. I’m getting the error *attempt to index nil with WaitForChild * coming from line 3 of this Local Script

the code

Client

game.ReplicatedStorage.Teleport.OnClientEvent:Connect(function(Player, Level)
	
	local leaderstats = Player:WaitForChild("leaderstats")
	
	if leaderstats.Level.Value >= Level then
		
		leaderstats.Level.Value += 1
	end
end)

Server

local TeleportEvent = game.ReplicatedStorage.Teleport
	
	local Part = script.Parent
	
local Teleport = Part.Teleport.Value
	
	local Level = Teleport:GetAttribute("Level")

Part.Touched:Connect(function(hit) 
		
		if hit.Parent:FindFirstChild("Humanoid") then 
			
			if game:GetService("Players"):FindFirstChild(hit.Parent.Name) then 
				
				local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	
				TeleportEvent:FireClient(Player, Level)
				
				task.wait(1)
				
				game:GetService("Players"):GetPlayerFromCharacter(hit.Parent).Character:WaitForChild("HumanoidRootPart").CFrame = Teleport.CFrame
			end
		end
	end)

FireClient doesn’t send the player as argument to OnClientEvent, you need to add the player as an argument, e.g.

TeleportEvent:FireClient(Player, Player, Level)

now I get 2 errors

You changed this TeleportEvent:FireClient(Player, Level) to this TeleportEvent:FireClient(Player, Player, Level)?

And second error is telling that you are trying to compare nil with number and this is probably because Level is nil

https://i.imgur.com/DyIUq2c.mp4

1 Like

Always use Players.LocalPlayer in local scripts when you want to get a reference to local player.
The second error means that Teleport does not have an attribute named “Level”.

(And don’t put vertical whitespaces after every line, it is hurting readability)

2 Likes

Please don’t try to get player like this. Use the method that I described above.

1 Like

I know that using LocalPlayer will be better, but I wanted answer to why this specific error happened

You didn’t answer why this specific error happened. What you did is you said to send additional player argument, which is a bad practice.

It does not send the first player argument as a parameter to OnClientEvent. Anyways, you could have just said to use Players.LocalPlayer.

1 Like