I’m making a leaderboard and everything is working except displaying the player’s name and stats. This is my script:
local scoreboard = script.Parent
local main = scoreboard.BG
local holder = main.StatsHold
local temp = holder.Template
local UIS = game:GetService("UserInputService")
local function clear()
for i,v in pairs(holder:GetChildren()) do
if v:IsA("Frame") then
v:Destroy()
end
end
end
local function generate()
for i,player in pairs(game.Players:GetPlayers()) do
local PS = player:FindFirstChild("PlayerStats")
local kills = PS.kills
local deaths = PS.deaths
local c = temp:Clone()
c.user.Text = player.Name
c.kills.Text = kills.Value
c.deaths.Text = deaths.Value
c.Parent = holder
end
end
Can anyone tell me why the values and names aren’t displaying?
Just to make sure, this is in a local script, right?
How are these functions being called? It may help to know whether they are being called at all.
On first glance, I don’t see anything explicitly wrong with the code. Is there code setting the position of the GUI elements?
Yes it is in a local script.
There is a server sided script that gather the values of the kills and deaths and the ui texts get updated to that as you can see in
local function generate()
for i,player in pairs(game.Players:GetPlayers()) do
local PS = player:FindFirstChild("PlayerStats")
local kills = PS.kills
local deaths = PS.deaths
local c = temp:Clone()
c.user.Text = player.Name
c.kills.Text = kills.Value
c.deaths.Text = deaths.Value
c.Parent = holder
end
end
If that’s the full script, then it seems that it does not call the generate or clear functions. This is why it doesn’t display the leaderboard stats. You’d need to do some work on the server side script for it to communicate to the players whenever the stats change.
Congratulations on joining the kool kids by using +=.
More seriously, the simplest way of handling this without having to deal with a massive number of connections would be to use a RemoteEvent.
-- Server Script
local remEvt = Instance.new("RemoteEvent")
remEvt.Name = "LeaderboardEvent"
remEvt.Parent = game.ReplicatedStorage
game.Players.PlayerAdded:Connect(function(plr)
local plrStats = Instance.new("Folder", plr)
plrStats.Name = "PlayerStats"
local Kills = Instance.new("IntValue", plr)
Kills.Name = "kills"
local deaths = Instance.new("IntValue", plr)
deaths.Name = "deaths"
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:FindFirstChild("Humanoid")
humanoid.Died:Connect(function()
deaths.Value += 1
remEvt:FireAllClients()
end)
end)
-- Local Script
local scoreboard = script.Parent
local main = scoreboard.BG
local holder = main.StatsHold
local temp = holder.Template
local remEvt = game.ReplicatedStorage:WaitForChild("LeaderboardEvent")
local generate = function()
for i,v in pairs(holder:GetChildren()) do
if v:IsA("Frame") then
v:Destroy()
end
end
for i,player in pairs(game.Players:GetPlayers()) do
local PS = player:FindFirstChild("PlayerStats")
local kills = PS.kills
local deaths = PS.deaths
local c = temp:Clone()
c.user.Text = player.Name
c.kills.Text = kills.Value
c.deaths.Text = deaths.Value
c.Parent = holder
end
end
remEvt.OnClientEvent:Connect(generate)
I refactored your code slightly; since your current set up has you clear the leaderboard each and every time you want to update it, it makes more sense to include the clear function at the start of the generate function.
This script isn’t perfect, though. I would suggest reworking it so that it simply updates the text of the leaderboard rather than totally refreshing it.
I believe we don’t need remote event because we can do on client side only. Anything changes on server get replicated on client also such as kill or death stats