Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't PlayerAdded fire?

I'm trying to use PlayerAdded but whenever I test in Play Solo it doesn't work! No output or anything!

game.Players.PlayerAdded:connect(function(Player)
	local h = Instance.new("Hint", game.Workspace)
	h.Text = Player.Name.." has entered the game!"
	game:GetService("Debris"):AddItem(h, 3)
end)

2 answers

✓ Accepted answer

In Play Solo mode, sometimes the player will actually join the game before the script runs. Thus, your function won't run, since the event was fired before it was set up.

You can avoid this by using Server+Player test mode, or explicitly running the function for Player1 if it exists at the time of execution.

2 comments

4

It also doesn't fire on local scripts, from what I hear.

1

You should also loop through each player and use the same code as in your connecting event before you connect your event incase the above described happens.

What I like to do is instead of anonymously connecting the PlayerAdded event to you function create a variable for it and then run it on each player that exists before the connection to PlayerAdded has been established. So yours would look something like this-

function PlayerAdded(Player)
    local h = Instance.new("Hint", game.Workspace)
    h.Text = Player.Name.." has entered the game!"
    game:GetService("Debris"):AddItem(h, 3)
end

for _, plr in pairs(Game.Players:GetPlayers()) do
	PlayerAdded(plr)
end

Game.Players.PlayerAdded:connect(PlayerAdded)

This discussion is preserved as it was. New answers, comments, and votes are closed.