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:
01 | game.Player.PlayerAdded:connect(plr) |
02 | local leaderstats = Instance.new( "Model" ,plr) |
03 | leaderstats.Name = "leaderstats" |
04 |
05 | local lvl = Instance.new( "IntValue" , leaderstats) |
06 | lvl.Name = "LVL" |
07 | lvl.Value = 1 |
08 |
09 | local currency = Instance.new( "IntValue" , leaderstats) |
10 | currency.Name = "Gold" |
11 | currency.Value = 100 |
12 | 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).
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 :
01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "Example" ) |
02 |
03 | game.Players.PlayerAdded:connect( function (player) |
04 | local key = "user_" .. player.userId |
05 | --we want to give 50 points to users each time they visit |
06 | DataStore:UpdateAsync(key, function (oldValue) |
07 | local newValue = oldValue or 0 --oldValue might be nil |
08 | newValue = newValue + 50 |
09 | return newValue |
10 | end ) |
11 | end ) |
````````
01 | local DS = game:GetService( "DataStoreService" ):GetDataStore( "Points" ) |
02 |
03 | game.Player.PlayerAdded:connect(plr) |
04 | local leaderstats = Instance.new( "Model" ,plr) |
05 | leaderstats.Name = "leaderstats" |
06 |
07 | local lvl = Instance.new( "IntValue" , leaderstats) |
08 | lvl.Name = "LVL" |
09 | lvl.Value = 1 |
10 |
11 | local currency = Instance.new( "IntValue" , leaderstats) |
12 | currency.Name = "Gold" |
13 | currency.Value = 100 |
14 |
15 | while wait( 5 ) do |
16 | DS:SetAsync(plr.userId.. "_DS" , currency.Value) |
17 | end |
18 | end ) |