Ad
Log in to vote
0

Why doesn't PlayerAdded fire? [closed]

Asked by
Everesty 975
3 years ago

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)

Locked by Everesty

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

2 answers

Log in to vote
2
Answered by 3 years ago

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.

4
It also doesn't fire on local scripts, from what I hear. Quenty 200 — 3y
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. Quenty 200 — 3y
Ad
Log in to vote
0
Answered by
Ekkoh 605
3 years ago

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)
Ad