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

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

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.

2 comments

0

Are you running this in a script or a localscript? If I remember right, PlayerAdded only fires in server-sided scripts.

0

This is in a script under Workspace.

2 answers

✓ Accepted answer

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!

4 comments

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.

0

I copied the size and position from your script, try making a gui the size you want, then just copy it's coordinates.

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.

0

I edited it and tested it, read it now.

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