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 » Scripting Helpers
Home Search
 

Re: Data Storage Help

Previous Thread :: Next Thread 
Krypticon is not online. 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 is not online. Krypticon
Joined: 12 Feb 2014
Total Posts: 680
19 Sep 2014 01:53 PM
Bump.
Report Abuse
Nyxis is not online. Nyxis
Joined: 15 Nov 2012
Total Posts: 3374
19 Sep 2014 01:54 PM
Bump. [2]
Report Abuse
Krypticon is not online. Krypticon
Joined: 12 Feb 2014
Total Posts: 680
19 Sep 2014 02:07 PM
Bump. [3]
Report Abuse
LegendaryAccount is not online. LegendaryAccount
Joined: 02 Jun 2010
Total Posts: 13193
19 Sep 2014 02:10 PM
The one that used data persistance works good
Report Abuse
Krypticon is not online. Krypticon
Joined: 12 Feb 2014
Total Posts: 680
19 Sep 2014 02:54 PM
Bump. [4]
Report Abuse
AhtnamasStudio is not online. AhtnamasStudio
Joined: 15 Jan 2014
Total Posts: 1104
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 is not online. 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 is not online. 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
FreeScriptMaker is not online. FreeScriptMaker
Joined: 29 Nov 2013
Total Posts: 2275
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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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