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

I need to make a script that saves a leaderstat, how do I do?

1 answer

✓ Accepted answer

In my opinion, the best way to store data for players is by using DataStore. You can learn more about that here: http://wiki.roblox.com/index.php?title=Data_store

I will put an example below. This is only for reference. You might be able to copy and paste my script, but you won't learn anything. I would recommend that you do some research and regard this as an example if you need help.

local DataStore = game:GetService("DataStoreService")
local GoldStore = DataStore:GetDataStore("GoldStore") -- You can call this whatever you want.

game.Players.PlayerAdded:Connect(function(Player)
local Leaderboard = Instance.new("Folder",Player)
local Gold = Instance.new("IntValue",Leaderboard)
Leaderboard.Name = "leaderstats"
Gold.Name = "Gold"
Gold.Value = GoldStore:GetAsync(Player.UserId) or 0
-- The code block above creates a simple leaderboard with gold. Whenever a player joins, their amount of gold is set to either A: The amount of gold saved last time they played or B: zero. The value of Gold will only be zero if the player has never saved a value for gold under their player ID in your game.

Gold.Changed:Connect(function()
GoldStore:SetAsync(Player.UserId, Gold.Value)
end)
-- The code block above makes sure that whenever the player's gold is modified, it saves it to the server.
game.Players.PlayerRemoving:Connect(function(Player)
local Gold = Player.leaderstats.Gold
GoldStore:SetAsync(Player.UserId, Gold.Value)
end)
-- Finally, when a player leaves, their gold is saved once more.

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