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)
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