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

How can I "refresh" the gui for all the players?

When the 1st player joins it shows 1 and when the second player joins it shows 2 for him but not for the first player... Any Help?

local Players = game:GetService("Players")
local plr = game.Players.LocalPlayer

function onPlayerAdded(player)
    print (#game.Players:GetChildren())
	plr.PlayerGui.ScreenGui.TextLabel.Text = #game.Players:GetPlayers()
end
 
--When a player joins, call the onPlayerAdded function
Players.LocalPlayer.CharacterAdded:connect(onPlayerAdded)
 
--Call onPlayerAdded for each player already in the game
for _,player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end

1 comment

0

is it in a local script ?

1 answer

Your problem is you are using functions that can only be used in either a LocalScript or a ServerScript

LocalPlayer can only be used in the LocalScript which is Client-sided

What I did was convert your script to run on the Server-side only, so make sure you put this in ServerScriptService

Code:

local Players = game:GetService("Players")

function UpdateText(number, plr)
	plr.PlayerGui.ScreenGui.TextLabel.Text = number
end

function onPlayerAdded(p)
	local totalplayers = #game.Players:GetChildren()
    print(totalplayers)
	for _,play in pairs(Players:GetPlayers()) do
    	UpdateText(totalplayers, play)
	end   
end

--When a player joins, call the onPlayerAdded function
Players.LocalPlayer.CharacterAdded:connect(onPlayerAdded)

--Call onPlayerAdded for each player already in the game
for _,player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end

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