Problem with this GUI i made

I made a LS Gui here is player 1
image
here is player 2
image
here is the Server Code
local Players = game:GetService(“Players”)

local StarterGui = game:GetService("StarterGui")

local PlayerJoined = game:GetService("ReplicatedStorage").PlayerJoined

local Server = StarterGui:WaitForChild("CustomLeaderstats").MainBackround.PlayerList

Players.PlayerAdded:Connect(function(Player)

wait()

local Money = game.ServerStorage.PlayerMoney[Player.Name]

PlayerJoined:FireAllClients(Player, Money.Value, false)

Money:GetPropertyChangedSignal("Value"):Connect(function()

PlayerJoined:FireAllClients(Player, Money.Value, true)

end)

end)

Players.PlayerRemoving:Connect(function(Player)

local Server = StarterGui:WaitForChild("CustomLeaderstats").MainBackround.PlayerList

Server[Player.Name]:Destroy()

end)

here is the client code

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer.PlayerGui
local PlayerJoined = game:GetService("ReplicatedStorage").PlayerJoined
local StarterGui = game:GetService("StarterGui")
local Templates = StarterGui.Templates
local PlayerTemplate = Templates.PlayerTemplate
wait()
local PlayerList = PlayerGui.CustomLeaderstats.MainBackround.PlayerList

PlayerJoined.OnClientEvent:Connect(function(Player, Stats, Debounce)
	if not Debounce then
		local PlayerStats = PlayerTemplate:Clone()
		PlayerStats.Parent = PlayerList
		local Money = PlayerStats:WaitForChild("Money")
		local PlayerName = PlayerStats:WaitForChild("Player")
		PlayerName.Text = Player.Name
		Money.Text = Stats
		PlayerStats.Name = Player.Name
	else
		local PlayerStats = PlayerList[Player.Name]
		local Money = PlayerStats:WaitForChild("Money")
		PlayerStats.Money.Text = Stats
	end
	
end)

Any advice on fixing it?

You would need to add any players already in the server when the client joins, along with listening for any players joining afterwards.

1 Like

I cant find anywhere how to do that, do you have a guide?

You would want to use a loop

for i,v in ipairs(game.Players:GetPlayers()) do
PlayerJoined:FireAllClients(Player, Money.Value, true)
end

I tried that it adds player2 twice.

You don’t really need remote events. Simply just use the given functions:

for i, player in pairs(game.Players:GetPlayers()) do -- This is to add any players that is already in the game
    -- add the players
end

game.Players.PlayerAdded:Connect(function(player)
    -- Add the player
end)

game.Players.PlayerRemoving:Connect(function(player)
    -- Remove the player
end)
1 Like

It adds Player2 twice because the server fired the remote and the client looked through Players to make a new entry in the leaderboard.

Use the loop upon joining. After the loop is run you can set up the listener for the Remote Event.

Check to see if the player is already on the leaderboard whenever that remote event is fired.

Thank you that worked! Really appreciate the help.