Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to save the leaderboard?

I'm making a simulator in Roblox but I don't know how to save the leaderboard stats. To clarify, if someone leaves and rejoins, they will have to start all the way from the beginning. This is the leaderboard script.

local DataStore = game:GetService("DataStoreService")
local PointsData = DataStore:GetOrderedDataStore("Points")

game.Players.PlayerAdded:Connect(function(player)
 
 local leaderstats = Instance.new("Folder")
 leaderstats.Name = "leaderstats"
 leaderstats.Parent = player
 
 local energy = Instance.new("NumberValue")
 energy.Name = "Energy"
 energy.Parent = leaderstats

 local coin = Instance.new("NumberValue")
 coin.Name = "LazerBux"
 coin.Parent = leaderstats

end)

2 comments

0

You would have to save their stats when they leave as well as load them when they join.

0

Yea but the thing is I don't know how to script.

2 answers

local DataStore = game:GetService("DataStoreService")
local PTS = DataStore:GetDataStore("PlayTimeStore")

game.Players.PlayerAdded:Connect(function(Player)
	
	local StatFolder = Instance.new("Folder",Player)
	StatFolder.Name = "leaderstats"
	
	local PT = Instance.new("IntValue",StatFolder) -- creating new value
	PT.Name = "PlayTime" -- changing name of value
	PT.Value = PTS:GetAsync(Player.UserId) or 0 -- loading saved value if player dont have any value then 0	
	print("Data Loaded!")
end)

game.Players.PlayerRemoving:Connect(function(Player)
	PTS:SetAsync(Player.UserId,Player.leaderstats.PlayTime.Value) -- saving data
	print("Data Saved!")
end)

this script is sample (script from my game btw) you can learn this script and make your own or get source from here

This discussion is preserved as it was. New answers, comments, and votes are closed.