Attempt to index nil with WaitForChild

I keep getting an error message saying " Attempt to index nil with WaitForChild" Sorry i suck at scripting lol

local player = game.Players.LocalPlayer
local Humanoid = player:WaitForChild("Humanoid")

script.Parent.ClickDetector.MouseClick:Connect(function()
 if Humanoid then
		if Humanoid.Health ~= 0 then
			local player = game.Players:GetPlayerFromCharacter()
			game.ReplicatedStorage.Remotes.OpenPetShop:FireClient(player)
	end
		end
end)
2 Likes

player is a localplayer, try player.Character

(Never mind, this is obviously serversided lol)

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local Humanoid = player.Character:WaitForChild("Humanoid")
    if Humanoid and Humanoid.Health ~= 0 then
        game.ReplicatedStorage.Remotes.OpenPetShop:FireClient(player)
    end
end)
1 Like

Make sure that you’re using a LocalScript when accessing game.Players.LocalPlayer. The “Local” in LocalPlayer means the player that belongs to the client running the LocalScript so it wouldn’t make sense to run it on the server.

Since you seem to be running it in a “Script” (on the server), I would recommend changing your code to this instead:

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local character = player.Character
    if character ~= nil then
        local humanoid = character:FindFirstChild("Humanoid")
		if humanoid ~= nil and humanoid.Health ~= 0 then
			game.ReplicatedStorage.Remotes.OpenPetShop:FireClient(player)
	    end
	end
end)

This allows you to detect the player that clicked it and fetch their character no matter what kind of script is running the code.

2 Likes