How do I make a GUI also appear for players that join after I run a command?

I’ve scripted a command that starts an event within the server. It displays the title, progress and a timer for how long the event has been running. Here’s what it looks like and how it works at the moment:


At the moment, the GUI doesn’t show for players that join after I run the command, only the players who were in the server at that moment, which isn’t ideal.

However, I’d also like it to show for players that join after the command is run, and players currently in the server. Including the timer’s progress, but this isn’t totally necessary.

if verbal == "Training" and trainings == false then
for i,v in pairs(game.Players:GetPlayers()) do
			
			local Training = v.PlayerGui.Trainings.TextLabel
			local Status = v.PlayerGui.Trainings.Line2
			local Timer = v.PlayerGui.Trainings.Line
			local HostName = Character.Name
			local TimerScript= game.StarterGui.Trainings.Line.LocalScript
	
			TimerScript.Disabled = false
			Status.Text = "Awaiting participants.."
			Event.Text = "TRAININGS"
			Timer.LocalScript.Disabled = false
			
			trainings = true
			
			end
		end

Here’s the server side which delivers the GUI to the players in the server - sorry in advance for my messy scripting.

Any ideas how I can achieve this, if it is possible?

Use the PlayerAdded event of Players to call the same code you do in the for loop.

1 Like

Hey, not sure if I’ve done this right but I added the game.Players.PlayerAdded to the for loop


	if verbal == "Training" and trainings == false then
		
		for i,v in pairs(game.Players:GetPlayers()) and game.Players.PlayerAdded do

And now the command won’t run at all. Here’s the console output.

Sorry if I’ve done something blatantly stupid, I’m not the best scripter lmao

So long as you’re trying to learn, we don’t care what your experience is.

Take the code that adds the GUI and make it a function like this:

local function add_gui(plr)
    local Training = plr.PlayerGui.Trainings.TextLabel
    local Status = plr.PlayerGui.Trainings.Line2
    local Timer = plr.PlayerGui.Trainings.Line
    local HostName = plr.Character.Name

    -- lines below this might have a problem, but we can deal with that in a second
    local TimerScript= game.StarterGui.Trainings.Line.LocalScript

    TimerScript.Disabled = false
    Status.Text = "Awaiting participants.."
    Event.Text = "TRAININGS"
    Timer.LocalScript.Disabled = false
    
    trainings = true
end

This allows the code to be re-used in multiple places.

Then adapt the for loop to use the function.

for i,v in pairs(game.Players:GetPlayers()) do
    add_gui(v)
end

Then connect the PlayerAdded event to use the same function.

game.Players.PlayerAdded:Connect(add_gui)
2 Likes

I believe that works! Thanks so much haha

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.