Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

PlayerAdded script won't work? (NOT YET SOLVED)

Asked by 9 years ago
game.Players.PlayerAdded:connect(function(player)
    local screengui = Instance.new('ScreenGui', game.Players.LocalPlayer.PlayerGui)
    local msg = Instance.new('TextLabel', screengui)
    msg.Text = Player.Name.." has entered the game! Welcome!"
    msg.Size = UDim2.new(0, 150, 0, 35)
    msg.Position = UDim2.new(0.5, -75, 0, 0)
    wait(5)
    screengui:remove()
end)

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

game.Players.PlayerAdded:connect(PlayerAdded)

When testing, the gui never shows up. No errors in Output or anything! I even tried extending the wait time so it would NEVER disappear, but the gui never shows up.

0
Are you running this in a script or a localscript? If I remember right, PlayerAdded only fires in server-sided scripts. TheGuyWithAShortName 673 — 9y
0
This is in a script under Workspace. IcyArticunoX 355 — 9y

2 answers

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

Several things wrong here.

First, game.Players.LocalPlayer only works in local scripts. However, the PlayerAdded event has the built-in parameter player, so use that instead.

Second, you're using an anonymous function, so the connection on line 15 and calling the function on line 12 will not work.

There are two ways you could fix this. The first way is to simply not use anonymous functions:

function PlayerAdded(player)
    local screengui = Instance.new('ScreenGui', player.PlayerGui)
    local msg = Instance.new('TextLabel', screengui)
    msg.Text = player.Name.." has entered the game! Welcome!" --Make sure 'player' is not capitalized! 
    msg.Size = UDim2.new(0, 150, 0, 35)
    msg.Position = UDim2.new(0.5, -75, 0, 0)
    wait(5)
    screengui:Destroy() --:remove() is deprecated, so it's better to use Destroy().
end

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

game.Players.PlayerAdded:connect(PlayerAdded)

Here is the other way:

 game.Players.PlayerAdded:connect(function(player)
        local screengui = Instance.new('ScreenGui', player.PlayerGui)
        local msg = Instance.new('TextLabel', screengui)
        msg.Text = player.Name.." has entered the game! Welcome!" --Make sure 'player' is not capitalized! 
        msg.Size = UDim2.new(0, 150, 0, 35)
        msg.Position = UDim2.new(0.5, -75, 0, 0)
        wait(5)
        screengui:remove()
end)

Also, when using a PlayerAdded event, you MUST start a server. PlayerAdded events DO NOT FIRE if you're trying to test in PlaySolo.

Hope I helped!

0
I would LOVE to say this problem is solved, but there's still a problem. Your script is AMAZING, and it does create the GUI. The problem is, the size and position are not set, so it is practically invisible... Once again, no errors in Output. IcyArticunoX 355 — 9y
0
I copied the size and position from your script, try making a gui the size you want, then just copy it's coordinates. Perci1 4988 — 9y
0
But the coordinates should be correct. It's in the script, but when the GUI is made, it doesn't have a size or a position. IcyArticunoX 355 — 9y
0
I edited it and tested it, read it now. Perci1 4988 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Err, does the same go for PlayerRemoving?

Answer this question