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.
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!