Krypticon
|
  |
| Joined: 12 Feb 2014 |
| Total Posts: 680 |
|
|
| 19 Sep 2014 01:38 PM |
Hey guys :D
I've been researching Data Storage, I've read the wiki, looked at some example open source places and experimented. I've managed to make some limited progress but I'm a bit confused. Could someone like give me an example for a script that would save someone's stats when they leave and load them when they join?
I'm pretty sure you would have to check if they have a save file yet and if not create a new one but yeah :P
I've attempted it and this is what I've got :)
DS=Game:GetService("DataStoreService")
Game.Players.PlayerAdded:connect(function(player) wait(1) local CreditsStore=DS:GetDataStore("CreditStore") end)
But then I gave up... :P
Can anyone explain it to me and possibly give me an example? Thanks :) |
|
|
| Report Abuse |
|
|
Krypticon
|
  |
| Joined: 12 Feb 2014 |
| Total Posts: 680 |
|
| |
|
Nyxis
|
  |
| Joined: 15 Nov 2012 |
| Total Posts: 3374 |
|
| |
|
Krypticon
|
  |
| Joined: 12 Feb 2014 |
| Total Posts: 680 |
|
| |
|
|
| 19 Sep 2014 02:10 PM |
| The one that used data persistance works good |
|
|
| Report Abuse |
|
|
Krypticon
|
  |
| Joined: 12 Feb 2014 |
| Total Posts: 680 |
|
| |
|
|
| 19 Sep 2014 02:59 PM |
DS = game:GetService("DataStoreService"):GetDataStore("Yourdatanamehere")
game.Players.PlayerAdded:connect(function(p) local H = Instance.new("NumberValue", p) H.Name = "Yourdatanamehere" --You can name it anything lol H.Value = DS:GetAsync(p.userId) H.Changed:connect(function() DS:UpdateAsync(p.userId, function(old) new = H return new.Value end) end) end)
game.Players.PlayerRemoving:connect(function(p) DS:UpdateAsync(p.userId, function(old) new = p:FindFirstChild("Yourdatanamehere") if new then return new.Value end end) end) |
|
|
| Report Abuse |
|
|
dbooted
|
  |
| Joined: 17 Oct 2008 |
| Total Posts: 5750 |
|
|
| 19 Sep 2014 03:11 PM |
IN ONE SCRIPT:
local DataStore = game:GetService("DataStoreService"):GetDataStore("Game") _G.ServerData = {}
--functionality function validate(localData, default) localData = localData or {} -- data -- return localData end
function load(userId) if DataStore ~= nil then local playerKey = tostring(userId) local data = DataStore:GetAsync(playerKey) or {} return data end end
function GetPlayerData(userId) if DataStore ~= nil then if _G.ServerData[tostring(userId)] ~= nil then local Data = _G.ServerData[tostring(userId)] return Data else _G.ServerData[tostring(userId)] = load(userId, {}) validate(_G.ServerData[tostring(userId)]) return _G.ServerData[tostring(userId)] end end end
function save(userId) if DataStore ~= nil then local data = GetPlayerData(userId) DataStore:SetAsync(tostring(userId), data) end end
--global calling _G.SetData = function(userId, data) _G.ServerData[tostring(userId)] = data end
_G.DataRequest = function(userId) local data = GetPlayerData(userId) or {} return data end
_G.SaveRequest = function(userId) save(userId) end ------------------------------------------------------
IN ANOTHER SCRIPT (EXAMPLE): game.Players.PlayerAdded:connect(function(player) local data = _G.DataRequest(player.userId) something.Value = data.something -- data -- end)
game.Players.PlayerRemoving:connect(function(player) local data = _G.DataRequest(player.userId) data.something = something.Value _G.SetData(player.userId, data) _G.SaveRequest(player.userId) end) |
|
|
| Report Abuse |
|
|
Krypticon
|
  |
| Joined: 12 Feb 2014 |
| Total Posts: 680 |
|
|
| 19 Sep 2014 03:12 PM |
| Thanks, but please may you explain how this works? Thanks :P |
|
|
| Report Abuse |
|
|
|
| 19 Sep 2014 03:25 PM |
local dataStoreService = game:GetService("DataStoreService") --get the DataStoreService
local datastore = dataStoreService:GetDataStore("dataStoreName") -- call the method "GetDataStore" from the DataStore service that returns a datastore with the name given
datastore:SetAsync("keyName", "value") --save data to the datastore with the key "keyName" and the value "value"
local val = datastore:GetAsync("keyName") --get the data print(val)
datastore:UpdateAsync(function(oldval) return "newValue" end) --UpdateAsync is used in a situation where you need the old value to know what the new value should be for example multiplying the old value by five
datastore:IncrementAsync("keyName", 5) --increment a value by the amount given |
|
|
| Report Abuse |
|
|
Krypticon
|
  |
| Joined: 12 Feb 2014 |
| Total Posts: 680 |
|
|
| 20 Sep 2014 08:50 AM |
Thanks everyone :D
@Free, yours helped me a lot thanks :)
:) |
|
|
| Report Abuse |
|
|