Custom leaderboard not working

Hello!

I am trying to make a leaderboard like in adopt me but it does not
work. I created a server script in ServerScriptService:

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local PlayerList = Players:GetChildren()
local y = 0


Players.PlayerAdded:Connect(function(player)
	for i,v in pairs(frame:GetChildren()) do
		v:Destroy()
	end
	for i,v in pairs(PlayerList) do
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = player.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50
	end
end)

this is what I mean

Any help would be appreciated.

1 Like

I believe you’re only saving the PlayerList once every time it’s called for, so it won’t update for other players when they join in later

Could you try putting that variable inside your PlayerAdded event instead?

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local y = 0


Players.PlayerAdded:Connect(function(player)
    local PlayerList = Players:GetPlayers()

	for i,v in pairs(frame:GetChildren()) do
		v:Destroy()
	end

	for i,v in pairs(PlayerList) do
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = v.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50
	end
end)

It does not work it did not create a label

Try again? From what I can see, you’re only passing the player parameter provided by the PlayerAdded event and not all of the individual Players using GetPlayers()

This may also depend if you’re using a ServerScript, when you should change it to a LocalScript instead

I changed to a local script and it still does not work.

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local y = 0


Players.PlayerAdded:Connect(function(player)
	local PlayerList = Players:GetPlayers()
	print(player)
	for i,v in pairs(frame:GetChildren()) do
		v:Destroy()
	end

	for i,v in pairs(PlayerList) do
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = player.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50

	end
end)

You’re still referencing the player.Name, which is resulting in cloning the frames’s Text Name to be that player that joined

Could you try this…?

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local y = 0


Players.PlayerAdded:Connect(function(player)
	local PlayerList = Players:GetPlayers()
	print(player)
	for i,v in pairs(frame:GetChildren()) do
		v:Destroy()
	end

	for i,v in pairs(PlayerList) do
        print(v.Name)
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = v.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50

	end
end)

It does not work in the local script.

Could you provide a screenshot & your Output? Something should’ve printed?

It does not show me anything in the output.

I edited my script and now it is a server script because local scripts can not see if a player joined.

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local PlayerList = Players:GetChildren()
local y = 0

Players.PlayerAdded:Connect(function(player)
	for i,v in pairs(PlayerList) do
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = player.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50
	end
end)

I added these prints it just printed once the top print.

local tem = game.ReplicatedStorage:WaitForChild("Template")
        local frame = game.StarterGui.ScreenGui.Frame
        local Players = game:GetService("Players")
        local PlayerList = Players:GetChildren()
        local y = 0

    Players.PlayerAdded:Connect(function(player)
    	print(player)
    	for i,v in pairs(PlayerList) do
    		print(v)
    		local newTem = tem:Clone()
    		newTem.Parent = frame
    		newTem.Text = Players.LocalPlayer.Name
    		newTem.Position = UDim2.new(0,0,0,y)
    		y += 50
    	end
    end)
        indent preformatted text by 4 spaces

Here’s the thing

If you’re changing the UI Objects from the Server-Side, it will not replicate to the client

Using a LocalScript inside a PlayerAdded event should work fine? I don’t see why it’s not working

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local y = 0
print("Just to make sure this script is working")

Players.PlayerAdded:Connect(function(player)
    print("Player added: "..player.Name)
	local PlayerList = Players:GetPlayers()

	for i,v in pairs(frame:GetChildren()) do
        print(v)
		v:Destroy()
	end

	for i,v in pairs(PlayerList) do
        print(v.Name)
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = v.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50

	end
end)

I used it in the local script in StarterGui
It only printed for me the first print

print("Just to make sure this script is working")

I added a remote event.
server:

Players.PlayerAdded:Connect(function(player)

game.ReplicatedStorage.RemoteEvent:FireClient(player)

end)

Client:

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local y = 0
print("Just to make sure this script is working")

--Players.PlayerAdded:Connect(function(player)
	
--end)

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(player)
	print("Player added: "..player.Name)
	local PlayerList = Players:GetPlayers()

	for i,v in pairs(frame:GetChildren()) do
		print(v)
		v:Destroy()
	end

	for i,v in pairs(PlayerList) do
		print(v.Name)
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = v.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50

	end
end)

Here’s what my assumption is then:

The LocalPlayer would already be loaded ahead of time since it’s a LocalScript, so the PlayerAdded event won’t work

I suppose to fix this, you could reference a local function & attempt to do this?

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local y = 0
print("Just to make sure this script is working")

local function ChangeLeaderboard(player)
    print("Player added: "..player.Name)
	local PlayerList = Players:GetPlayers()

	for i,v in pairs(frame:GetChildren()) do
        print(v)
		v:Destroy()
	end

	for i,v in pairs(PlayerList) do
        print(v.Name)
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = v.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50

	end
end)

Players.PlayerAdded:Connect(ChangeLeaderboard)

for _, Player in pairs(game.Players:GetPlayers()) do
    ChangeLeaderboard(Player)
end

It is the other way around. If you change the UI in the client, it will not replicate to the server. Anything changed in the server will be changed in the client. However, it is must more suggested to handle GUIs in a local script to decrease the workload of the server.

@superfoose5 You did not state your problem. We cannot identify the solutions when we don’t know what the problem is.

Edited: Seems like it is now working.

It works thanks for all the help!

Bit confusing, but I understand your point

I just find it easier to reference it that way since there’s some instances where members use Scripts inside the GuiObjects, which would only be replicated to the server side (And wouldn’t change to the client)

Referencing the player from a server-side though I fully understand that