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 » Game Design
Home Search
 

Re: Saving Values

Previous Thread :: Next Thread 
TheLlamaOfGB is not online. TheLlamaOfGB
Joined: 14 Apr 2009
Total Posts: 92
17 Nov 2014 01:30 PM
Hi,

I've recently been making several features for a group game.
One feature is money, which is stored in the PlayerGui as a value as ease of access.
Is there any way I can create a script to save the value to the player, so that they rejoin any server having kept the value?
(I'm inexperienced with any saving method, so please don't jump right into it, I'm not a great scripter!)

Any help is much appreciated
Report Abuse
UnConcerning is not online. UnConcerning
Joined: 30 Aug 2012
Total Posts: 676
17 Nov 2014 01:35 PM
DataStore service
Report Abuse
TheLlamaOfGB is not online. TheLlamaOfGB
Joined: 14 Apr 2009
Total Posts: 92
18 Nov 2014 01:32 PM
Thanks, but how do I use that? Would it also allow me to keep the values when the player died?
Report Abuse
Caedus1x1x1x1 is not online. Caedus1x1x1x1
Joined: 22 Sep 2011
Total Posts: 258
18 Nov 2014 01:36 PM
I might could just script it for you. I'm sick of explaining this,it's a complicated subject.
Report Abuse
MemoryAddress is not online. MemoryAddress
Joined: 20 Apr 2013
Total Posts: 1297
18 Nov 2014 01:44 PM
I'll assume you know how to set the player's money and get it in the server. Here, the variables used are 'player', which is the player you're saving for, and 'money', which is that player's money.

To use a DataStore, you want to first get one, which is done using DataStoreService's GetGlobalDataStore and GetDataStore methods. I'm going to use GetGlobalDataStore.

local ds = game:GetService("DataStoreService"):GetGlobalDataStore()

This datastore is 'synced' across all servers, though it may take a bit for that to catch up. Anything saved in any object returned by GetGlobalDataStore will be accessible across all servers in that game.

So, to set a value in the datastore, you use the datastore's SetAsync method, which takes a string as the key, and a value. This value can be a string, a number, or a table.

ds:SetAsync(player.userId.."Money", money)

And you're done. It's saved.

Now to get a player's money, you use the datastore's GetAsync method, which takes only a key. If the value hasn't been set before, it'll return nil.

local retrievedMoney = ds:GetAsync(player.userId.."Money") or 0

From here, you can set the value in the game.

To save a player's money when they leave, you'd use something like this:
local ds = game:GetService("DataStoreService"):GetGlobalDataStore()
game.Players.PlayerRemoving:connect(function(player)
-- get the player's money before this line, and set it to the variable 'money'
ds:SetAsync(player.userId.."Money", money)
end)

And to load it when they join, you'd use something like this:
local ds = game:GetService("DataStoreService"):GetGlobalDataStore()
game.Players.PlayerAdded:connect(function(player)
local money = ds:GetAsync(player.userId.."Money")
-- set the player's money leaderstat or otherwise display it
end)

Why don't you want to just use a DataStore all the time? DataStores have some limitations. The ones that you'd run into here (probably):
- They have a request limit equal to 60 + 10 * (number of players in the server).
- They can't be used on the client (in a LocalScript).

For more information on DataStores, see these pages:
http://wiki.roblox.com/index.php?title=Data_store
http://wiki.roblox.com/index.php?title=API:Class/DataStoreService
http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Game Design
   
 
   
  • 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