Code Review
The Problem
Hello, so I’m making a custom leaderboard, and it works with no lag but I still feel like this isn’t the most performant way to do this, especially with the opening/closing.
It has an activity 0.75%-1.1% by 1 player opening and closing it 40/s (which will very rarely/never happen likely). It is a ServerScript.
I still feel like its a little too high for just 1 player. Thoughts?
Code
(i added indents for easier reading)
serverside code:
workspace:WaitForChild("Scripts")
--for the nice number visuals
local req = require(workspace.Scripts:WaitForChild("NumberAbbreviator"))
game.Players.PlayerAdded:Connect(function(add)
--waiting so the script doesnt error
add:WaitForChild("leaderstats")
add.leaderstats:WaitForChild("Kills")
add.leaderstats:WaitForChild("Deaths")
add.leaderstats:WaitForChild("Time")
--so the datastores have time to load in if its very laggy
task.wait(0.5)
--template stuff
local clo = game.ServerStorage.lbtemplate:Clone()
clo.Name = add.Name
clo.name.Text = add.Name
clo.kills.Text = req:AbbreviateNumbers(add.leaderstats.Kills.Value)
clo.deaths.Text = req:AbbreviateNumbers(add.leaderstats.Deaths.Value)
clo.time.Text = req:AbbreviateNumbers(add.leaderstats.Time.Value)
clo.Parent = game.ServerStorage.leaderboard
--updating for change
add.leaderstats.Kills:GetPropertyChangedSignal("Value"):Connect(function()
local num = req:AbbreviateNumbers(add.leaderstats.Kills.Value)
game.ServerStorage.leaderboard:FindFirstChild(add.Name).kills.Text = num
end)
add.leaderstats.Deaths:GetPropertyChangedSignal("Value"):Connect(function()
local num = req:AbbreviateNumbers(add.leaderstats.Deaths.Value)
game.ServerStorage.leaderboard:FindFirstChild(add.Name).deaths.Text = num
end)
add.leaderstats.Time:GetPropertyChangedSignal("Value"):Connect(function()
local num = req:AbbreviateNumbers(add.leaderstats.Time.Value)
game.ServerStorage.leaderboard:FindFirstChild(add.Name).time.Text = num
end)
--open/close
game.ReplicatedStorage.lbopen.OnServerEvent:Connect(function(plr,db)
if db == true then
plr.PlayerGui.Leaderboard.ic1.Visible = true
plr.PlayerGui.Leaderboard.ic2.Visible = true
plr.PlayerGui.Leaderboard.ic3.Visible = true
local clo = game.ServerStorage.leaderboard:Clone()
clo.Parent = plr.PlayerGui.Leaderboard
else
if plr.PlayerGui.Leaderboard:FindFirstChild("leaderboard") then
plr.PlayerGui.Leaderboard:FindFirstChild("leaderboard"):Destroy()
end
plr.PlayerGui.Leaderboard.ic1.Visible = false
plr.PlayerGui.Leaderboard.ic2.Visible = false
plr.PlayerGui.Leaderboard.ic3.Visible = false
end
end)
end)
--destroy the template when the plr leaves
game.Players.PlayerRemoving:Connect(function(add)
if game.ServerStorage.leaderboard:FindFirstChild(add.Name) then
game.ServerStorage.leaderboard:FindFirstChild(add.Name):Destroy()
end
end)
--lor5s
local code:
game:GetService("UserInputService").InputBegan:Connect(function(inp)
if inp.KeyCode == Enum.KeyCode.Tab then
game.ReplicatedStorage.lbopen:FireServer(true)
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(inp)
if inp.KeyCode == Enum.KeyCode.Tab then
game.ReplicatedStorage.lbopen:FireServer(false)
end
end)