|
| 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 |
|
| |
|
| 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 |
|
|
| 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 |
|
|
| 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 |
|