game.Players.PlayerAdded:connect(function(player) script.Parent.Text = player.Name .. " has entered" end)
Well, I was trying to make a script where, when a player entered, the text said:
player.Name .. " has entered"
I tried like 4 - 5 different types of ways to do this, but it just wasn't working out for me. I also tried to make it where, let's say you joined a game, and it said welcome then your name on the text, and for other people it said your name, and that you have joined. I couldn't figure that out either... Can you help me?
I don't see anything wrong with your code, but if you're using the playeradded event, make sure the script is a regular script, not a localscript. Also, make sure it's parented to gui/guiobject you want to change the text of. If you're using a localscript with a gui in it then you should have a serverscript that fires a remote event to a localscript which then changes the text of the gui everytime a player joins the game. (You can also use the childadded event in the player property if you're using a local script but i wouldnt really recommend it)
01 | --- If your script is a localscript parented under a gui |
02 | -- Serverscript (regular script) |
03 | local event = game:GetService 'ReplicatedStorage' :FindFirstChild 'RemoteEvent' |
04 | if not event then |
05 | event = Instance.new( "RemoteEvent" ,game:GetService 'ReplicatedStorage' ) |
06 | end |
07 | game:GetService 'Players' .PlayerAdded:connect( function (p) |
08 | event:FireAllClients(p.Name) |
09 | end ) |
10 |
11 | -- localscript parented under the gui |
12 | repeat wait() until game.Players.LocalPlayer and game:GetService 'ReplicatedStorage' :FindFirstChild 'RemoteEvent' |
13 | local event = game:GetService 'ReplicatedStorage' :FindFirstChild 'RemoteEvent' |
14 | local gui = script.Parent |
15 |
I don't think there is anything wrong with your code, but make sure you are NOT using a LocalScript
with the PlayerAdded
event.
01 | -- This code can only be used in a regular script. |
02 |
03 | tl = game.ReplicatedStorage.ScreenGuiOfEpicness.TextLabelOfFaith -- Change this to the TextLabel |
04 |
05 | function playerAdded(newPlayer) |
06 | tl.Visible = true |
07 | tl.Text = newPlayer.Name .. "has entered the server!" |
08 | wait( 2 ) |
09 | tl.Text = "" |
10 | tl.Visible = false |
11 | end |
12 |
13 | game.Players.PlayerAdded:connect(playerAdded) |
As you can see, the code will obviously work. It's preferred to use BindableEvents
to pass this onto another Script
, which can then trigger this on each client.