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

How do I save and load leaderboard stats?

How do I save the values/string of a leaderboard stat and have it load the next time the player joins the game? The following is the script I will be using for my game:

game.Player.PlayerAdded:connect(plr)
	local leaderstats = Instance.new("Model",plr)
	leaderstats.Name = "leaderstats"

	local lvl = Instance.new("IntValue", leaderstats)
	lvl.Name = "LVL"
	lvl.Value = 1

	local currency = Instance.new("IntValue", leaderstats)
	currency.Name = "Gold"
	currency.Value = 100
end)

I would like you to edit this script so that it could save and load the next time the player joins. I would also like you to elaborate the workings of saving and loading values (when I tried to check the roblox wiki, something about Data store came up but I don't really understand).

1 answer

Heres to save, load data.. http://wiki.roblox.com/index.php?title=DataStore

This should teach you about saving / loading data. ;D

http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore/UpdateAsync ROBLOX's example :

local DataStore = game:GetService("DataStoreService"):GetDataStore("Example")
 
game.Players.PlayerAdded:connect(function(player)
	local key = "user_" .. player.userId
	--we want to give 50 points to users each time they visit
	DataStore:UpdateAsync(key, function(oldValue)
		local newValue = oldValue or 0 --oldValue might be nil
		newValue = newValue + 50
		return newValue
	end)
end)
~~~~~~~~~~~~~~~~~

local DS = game:GetService("DataStoreService"):GetDataStore("Points")

game.Player.PlayerAdded:connect(plr)
    local leaderstats = Instance.new("Model",plr)
    leaderstats.Name = "leaderstats"

    local lvl = Instance.new("IntValue", leaderstats)
    lvl.Name = "LVL"
    lvl.Value = 1

    local currency = Instance.new("IntValue", leaderstats)
    currency.Name = "Gold"
    currency.Value = 100

   while wait(5) do
	DS:SetAsync(plr.userId.."_DS", currency.Value)
  end
end)
~~~~~~~~~~~~~~~~~

14 comments

0

Thanks! :D

0

Can you atleast give me what I wanted for my script?

0

Sure.

0

Question: for the while wait(5) do part, does that save the currency value every 5 seconds and not everything else?

0

Yea, you would have to set up nother datastore table or something to make it save the lvl.

0

So... Basicly the same as lines 15-17, except that instead of currency.Value, it's lvl?

0

yes

0

Thanks! :D

0

np

0

Accept my answer please! :D

0

4 Years And Your Answer Wasn't Accepted But Works.

1

Another year goes by and his answer still hasn't been accepted, sadness.

0

Truth CodedStars lol.

0

Truth CodedStars lol.

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