|
| 21 Aug 2017 09:50 AM |
Trying out datastores for my place for the first time. On studio it works completely fine but when tried on the client it does not work at all.
Is this to do with studio wrapping everything as a LocalScript or is it because I've put it in ServerScriptService
local DS = game:GetService("DataStoreService") local DSName = DS:GetDataStore("DLROysterCredits")
game.Players.PlayerAdded:connect(function(player) local backpack = player:WaitForChild('Backpack') local cash = backpack.Oyster.Balance local free = backpack.Oyster.ClaimedFreeCredits cash.Value = DSName:GetAsync(player.UserId) or 0 DSName:SetAsync(player.UserId, cash.Value) cash.Changed:connect(function() print("Saving new data...") DSName:SetAsync(player.UserId, cash.Value) print(player.UserId.."'s balance of £"..cash.Value.."has been saved !") end) end) |
|
|
| Report Abuse |
|
|
Ajastra
|
  |
| Joined: 01 Aug 2017 |
| Total Posts: 1461 |
|
|
| 21 Aug 2017 09:55 AM |
You shouldn't be saving a player's cash by itself, all player data should be packed into a single table and saved.
You should not get / set the data of guests. To check if a player is a guest, remember that guests will always have a player.UserId < 1.
Additionally, you shouldn't be saving when the player joins or when the cash changes. You should only save when the player leaves, the player makes a R$ purchase, the game shuts down (with game:BindToClose()), and auto-save player data every few minutes.
I personally advise you to pcall calls to potentially unreliable APIs such as DataStores. A good example of this is the Saving Player Data article on the Wiki. I also make sure that the data could be loaded successfully if it existed when I check if I should save data.
|
|
|
| Report Abuse |
|
|
Ajastra
|
  |
| Joined: 01 Aug 2017 |
| Total Posts: 1461 |
|
|
| 21 Aug 2017 09:58 AM |
Also, LocalScripts won't execute on the server, and it works in Studio because in Studio Play Solo testing there is no separation between client and server. For proper networking, use Test Server. I also recommend using Diabolical Mode to test, it is a new feature that will make requests error on random to test your script's data safety.
This should be done server-side in ServerScriptService with a Script.
|
|
|
| Report Abuse |
|
|
|
| 21 Aug 2017 11:39 AM |
| Tried all of that, wiki really didn't help and the pcall wiki entry was really bland. |
|
|
| Report Abuse |
|
|
Ajastra
|
  |
| Joined: 01 Aug 2017 |
| Total Posts: 1461 |
|
|
| 21 Aug 2017 11:41 AM |
Here's a good tutorial:
http://wiki.roblox.com/index.php?title=Saving_Player_Data
Here's how to run code when the game will shutdown (you can delay for up to 30 seconds, a DataStore:SetAsync() call is about ~1 second IIRC, so you mostly should be good):
http://wiki.roblox.com/index.php?title=API:Class/DataModel/BindToClose
And here's a good reference on DataStores:
http://wiki.roblox.com/index.php?title=Data_store
|
|
|
| Report Abuse |
|
|