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