Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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
0
is it in a local script ? TigerClaws454 48 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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
Ad

Answer this question