.PlayerAdded doesnt work?

I’m trying to detect the player being added so i can use .CharacterAdded to get the humanoid and then use Humanoid.Died so the player cant use a tool after they die. .PlayerAdded isn’t working in-game or in studio.

This script is located inside of a sword tool.

My script isn’t a localscript its a server script.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		character:WaitForChild("Humanoid").Died:Connect(function()
			
			alive = false
			print("died")
			
		end)
	end)
end)

instead of doing this whole thing, just check the humanoid’s health. it’s always 0 when the humanoid is dead.
example:

if character.Humanoid.Health <= 0 then return end

the script isn’t a localscript so it wouldn’t work, I made my sword script as a serverside script, it says on the developer hub that humanoid.health isn’t replicated in serverside.

Keep in mind that since the playeradded event is in the tool, the event will be created AFTER the player joins the game which means the event wont run when the player holding the tool joins the game, only everyone after them.

Is this what you want?

What you could do instead of having the tool in the StarterPack is give the player the tool (by putting the tool in their character) whenever you want them to have it (i.e when a round starts) that way they won’t have the tool again when they die.

3 Likes

Try looping through each player and calling it. Sometimes the player loads before the event signal connects.

local function OnCharacterAdded(character)
    local humanoid = character:FindFirstChild("Humanoid")

    humanoid.Died:Connect(function()
        alive = false
        print("died")
    end)
end

local function OnPlayerAdded(player)
    player.CharacterAdded:Connect(OnCharacterAdded)
end

for _, player in pairs(game.Players:GetPlayers()) do
    OnPlayerAdded(player)
end

game.Players.PlayerAdded:Connect(OnPlayerAdded)

The script is most likely not running before the PlayerAdded event is firing, and therefore, the event listener has not been created at the time that the event is being fired. You’ll most likely have to reconsider your approach here. You can’t use PlayerAdded to obtain a reference to a player through a script that is created for that player after they join.