Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

Please I need to know

0
Do you mean saving it to DataStore? TheeDeathCaster 2368 — 6y
0
yeah starwars5319 7 — 6y
0
Alright, here's the manual: http://wiki.roblox.com/index.php?title=Data_store :) TheeDeathCaster 2368 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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.
Ad

Answer this question