How to make a time leaderstats on roblox studio

Опубликовано: 08 Сентябрь 2026
на канале: Beanie
158
4

local Players = game:GetService("Players")

local function setupLeaderstats(player)
-- Check if leaderstats already exists, if not create it
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end

-- Check if Seconds stat already exists, if not create it
local secondsStat = leaderstats:FindFirstChild("Seconds")
if not secondsStat then
secondsStat = Instance.new("IntValue")
secondsStat.Name = "Seconds"
secondsStat.Value = 0
secondsStat.Parent = leaderstats
end

-- Function to increment the Seconds stat every second
local function incrementSeconds()
while true do
task.wait(1) -- Wait for 1 second
secondsStat.Value = secondsStat.Value + 1
end
end

-- Start the incrementing in a new thread
task.spawn(incrementSeconds)
end

-- Connect the setupLeaderstats function to the PlayerAdded event
Players.PlayerAdded:Connect(setupLeaderstats)

-- Iterate through all existing players and setup their leaderstats
for _, player in Players:GetPlayers() do
setupLeaderstats(player)
end