What this script does
Create the standard leaderstats folder from a defaults table, then update named numeric stats through one small server-side API.
- Best for
- Coins, Speed, wins, stages, and other visible stats in a Studio experience you own.
- Language
- Luau
- Version
- 1.0.0
- Validation
- Luau checked · Studio integration required
Official Luau 0.735 parsing and static analysis pass with Roblox host types stubbed. Test the actual Instance, service, or event integration in a separate Studio place before production use.
Included behavior
- Creates a standard leaderstats Folder
- Supports any numeric defaults table
- Updates existing NumberValue stats safely
Install in the stated runtime
- 1Create a ModuleScript named Leaderstats.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Call create when PlayerAdded fires, then update values only from trusted server gameplay.
Minimal usage example
local Players = game:GetService("Players")
local Leaderstats = require(script.Parent.Leaderstats)
Players.PlayerAdded:Connect(function(player)
Leaderstats.create(player, { Coins = 0, Speed = 0 })
end)Example inputs and result
- Supply
- A Player object and your own initial stat names and values.
- Expect
- Creates that player’s leaderstats in your Studio experience.
These notes explain the example above. Read the complete source for every exported function and edge case before integrating it.
Complete source
No hidden import, minified block, or shortened loader.
--!strict
-- MIT License. Server-side leaderstats helper for experiences you own or edit.
local Leaderstats = {}
function Leaderstats.create(player: Player, defaults: { [string]: number }): Folder
local existing = player:FindFirstChild("leaderstats")
if existing and existing:IsA("Folder") then return existing end
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
for name, value in defaults do
local stat = Instance.new("NumberValue")
stat.Name = name
stat.Value = value
stat.Parent = folder
end
return folder
end
function Leaderstats.set(player: Player, name: string, value: number): boolean
local folder = player:FindFirstChild("leaderstats")
local stat = folder and folder:FindFirstChild(name)
if not stat or not stat:IsA("NumberValue") then return false end
(stat :: NumberValue).Value = value
return true
end
return table.freeze(Leaderstats)
eea29b8046c601991d9412d984dbaec19063847042b52a24ebf368de04165362Outside this script’s scope
- Persist values between sessions
- Accept unvalidated client totals
Found a problem or a better approach?
Tell us what failed, what you expected, and whether the issue happened in the Luau CLI or Roblox Studio. Suggestions for clearer types, safer server checks, or a more useful API are welcome.