|
| 01 Jun 2014 02:13 PM |
With the old Data Persistence system, do I have to worry about deleting keys? Like, if they reset thier score to 0, is it ok if the data becomes saved at 0, instead of blank data?
Also, how would I set up a Player leaving/rejoining saving system with the new DataStores? I want to use the DataStores since the old Data Persistence is not recommended for this, but idk how to use the DataStores this way. Can you help? Thanks! |
|
|
| Report Abuse |
|
|
| |
|
Ultraw
|
  |
| Joined: 20 Nov 2010 |
| Total Posts: 6575 |
|
|
| 02 Jun 2014 09:55 AM |
I did this for a basic "score" value in leaderstats. I dont know how you want to do it.
datastoreName = "Score"
ds = game:GetService("DataStoreService"):GetDataStore(datastoreName)
game.Players.PlayerEntered:connect(function(p) if ds:GetAsync("user_"..p.Name) then p.leaderstats.Score.Value = ds:GetAsync("user_"..p.Name) end end
game.Players.PlayerRemoving:connect(function(p) ds:SetAsync("user_"..p.Name,p.leaderstats.Score.Value end
|
|
|
| Report Abuse |
|
|
Ultraw
|
  |
| Joined: 20 Nov 2010 |
| Total Posts: 6575 |
|
|
| 02 Jun 2014 09:55 AM |
Sorry..
datastoreName = "Score"
ds = game:GetService("DataStoreService"):GetDataStore(datastoreName)
game.Players.PlayerEntered:connect(function(p) if ds:GetAsync("user_"..p.Name) then p.leaderstats.Score.Value = ds:GetAsync("user_"..p.Name) end end)
game.Players.PlayerRemoving:connect(function(p) ds:SetAsync("user_"..p.Name,p.leaderstats.Score.Value end)
|
|
|
| Report Abuse |
|
|
|
| 02 Jun 2014 10:10 AM |
| I see. The issue I have is I want to save several values, not just 1, like Score, Money, and Level. Is there a way to do this? |
|
|
| Report Abuse |
|
|
Ultraw
|
  |
| Joined: 20 Nov 2010 |
| Total Posts: 6575 |
|
|
| 02 Jun 2014 10:16 AM |
level = game:GetService("DataStoreService"):GetDataStore("Level") money = game:GetService("DataStoreService"):GetDataStore("Money") score = game:GetService("DataStoreService"):GetDataStore("Score")
game.Players.PlayerEntered:connect(function(p) if level:GetAsync("user_"..p.Name) then p.leaderstats.Level.Value = level:GetAsync("user_"..p.Name) end if money:GetAsync("user_"..p.Name) then p.leaderstats.Money.Value = money:GetAsync("user_"..p.Name) end if score:GetAsync("user_"..p.Name) then p.leaderstats.Score.Value = score:GetAsync("user_"..p.Name) end end) end)
game.Players.PlayerRemoving:connect(function(p) level:SetAsync("user_"..p.Name,p.leaderstats.Level.Value) money:SetAsync("user_"..p.Name,p.leaderstats.Money.Value) score:SetAsync("user_"..p.Name,p.leaderstats.Score.Value) end) |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2014 10:26 AM |
| Wouldn't that make the Level, Score, and Money all save and load as the same value though, or am I missing something? |
|
|
| Report Abuse |
|
|
Ultraw
|
  |
| Joined: 20 Nov 2010 |
| Total Posts: 6575 |
|
|
| 02 Jun 2014 10:26 AM |
No, they are all different DataStores.
|
|
|
| Report Abuse |
|
|
|
| 02 Jun 2014 10:27 AM |
p.leaderstats.Score.Value = score:GetAsync("user_"..p.Name)
No
It gwts a global datastore, and sets a specificly named key to a value |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2014 10:41 AM |
| I recommend staying away from player leaving save system by itself. You should also save data periodically while the server is running. If you do use player leave to save data, also make sure that you do not overwrite their save data if they leave as soon as they join (for example, if a player disconnects while loading, and all their stat values have been created, but the stats were not loaded from the data store yet, the default values (typically 0) will be saved OVER their actual previous values). |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2014 10:46 AM |
| Ohhhh, so basically, you have 3 DataStores for each value, and then save the Player's name in each one to keep track of how much they have for each value. Cool :D. |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2014 10:50 AM |
| Ok. How would I make sure that the data doesn't overwrite if the player leaves before the game loads? |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2014 10:53 AM |
I also recommend using one data store (named 'Stats' or something) and save a table instead of wasting your time with three data stores. Also, save by userId, not player Name (players can change their names now).
local stats = game:GetService("DataStoreService"):GetDataStore("Stats")
game.Players.PlayerEntered:connect(function(p) local pstats = stats:GetAsync("user_"..p.userId) if pstats then p.leaderstats.Level.Value = pstats.Level p.leaderstats.Money.Value = pstats.Money p.leaderstats.Score.Value = pstats.Score end end)
end)
game.Players.PlayerRemoving:connect(function(p) stats:SetAsync("user_"..p.userId, { Level = p.leaderstats.Level.Value, Money = p.leaderstats.Money.Value, Score = p.leaderstats.Score.Value }) end)
Though I do highly recommend using UpdateAsync instead of SetAsync, and remember my old post about making sure default data isn't overwritten. |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2014 11:01 AM |
| Ok thanks. How do I make sure that the default data isn't overwritten? |
|
|
| Report Abuse |
|
|
| |
|
| |
|