generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Datastore script works in studio but not on client

Previous Thread :: Next Thread 
VirtualHat is not online. VirtualHat
Joined: 21 Feb 2011
Total Posts: 717
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 is not online. 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 is not online. 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
VirtualHat is not online. VirtualHat
Joined: 21 Feb 2011
Total Posts: 717
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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image