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!"
msg.Size = UDim2.new(0, 150, 0, 35)
msg.Position = UDim2.new(0.5, -75, 0, 0)
wait(5)
screengui: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!"
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!
2 comments
Are you running this in a script or a localscript? If I remember right, PlayerAdded only fires in server-sided scripts.
This is in a script under Workspace.