Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 10 Feb 2016 11:12 AM |
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats") print("cash datastore created") game.Players.PlayerRemoving:connect(function(player) print("ExitedGame") player:WaitForDataReady() print("data ready") wait(2) local stats = player:FindFirstChild("leaderstats") datastore:SetAsync("Cash", stats.Cash.Value) print("Saved stats") end)
game.Players.PlayerAdded:connect(function(newPlayer) print("EnteredGame") newPlayer:WaitForDataReady() print("data ready") wait(2) local stats2 = newPlayer:FindFirstChild("leaderstats") stats2.Cash.Value = datastore:GetAsync("Cash") print("Loaded stats") end) |
|
|
| Report Abuse |
|
|
davisky2
|
  |
| Joined: 04 Mar 2012 |
| Total Posts: 4710 |
|
|
| 10 Feb 2016 11:15 AM |
I dont think that 2 seconds will be short enough time when the player is leaving/removing to save data.
~davisky~ |
|
|
| Report Abuse |
|
|
Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 10 Feb 2016 11:16 AM |
| yeah thats kinda a problem Im having, I commented out both wait for data and the wait and all it was able to get to was the first print statement |
|
|
| Report Abuse |
|
|
davisky2
|
  |
| Joined: 04 Mar 2012 |
| Total Posts: 4710 |
|
|
| 10 Feb 2016 11:17 AM |
I would better suggest an autosave.
~davisky~ |
|
|
| Report Abuse |
|
|
Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 10 Feb 2016 11:30 AM |
| Im trying to make one but I dont think im getting anywhere with it. Do you have any ideas/examples I could use |
|
|
| Report Abuse |
|
|
Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 10 Feb 2016 11:37 AM |
I made a main script for when the player enters:
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats") print("cash datastore created")
game.Players.PlayerAdded:connect(function(newPlayer) print("EnteredGame") --newPlayer:WaitForDataReady() wait(5) print("data ready") local stats2 = newPlayer:FindFirstChild("leaderstats") stats2.Cash.Value = datastore:GetAsync("Cash") print("Loaded stats") local s = script.autosave:clone() s.Parent = newPlayer.Character s.Disabled = false print("added autosave script to player") end)
And a local script inside of it to autosave:
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats") local player = game.Players.LocalPlayer local stats = player:FindFirstChild("leaderstats") wait(10)
while true do datastore:SetAsync("Cash", stats.Cash.Value) print("Saved stats") wait(1) end
However It still doesnt seem to be working, can you see if I'm doing anything wrong? |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2016 12:33 PM |
I wouldn't use SetAsync that often in the autosave, read http://wiki.roblox.com/index.php?title=Data_store for limitations. SetAsync can only have 60 + numPlayers * 10 requests per minute.
|
|
|
| Report Abuse |
|
|
Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 10 Feb 2016 12:42 PM |
this is my rebuilt script (works in studio testing)
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats") print("cash datastore created")
game.Players.PlayerAdded:connect(function(newPlayer) print("EnteredGame") --newPlayer:WaitForDataReady() wait(5) print("data ready") local stats = newPlayer:FindFirstChild("leaderstats") stats.Cash.Value = datastore:GetAsync(newPlayer.userId .. "Cash") print("Loaded stats") datastore:SetAsync(newPlayer.userId .. "Cash", stats.Cash.Value) print("Saved stats") wait(1) while true do datastore:SetAsync(newPlayer.userId .. "Cash", stats.Cash.Value) print("Saved stats") if newPlayer.Character.Humanoid.Health == 0 then print("character died") newPlayer.leaderstats.Resources.Value = newPlayer.leaderstats.Resources.Value / 4 end wait(.5) end end) |
|
|
| Report Abuse |
|
|
davisky2
|
  |
| Joined: 04 Mar 2012 |
| Total Posts: 4710 |
|
|
| 10 Feb 2016 12:48 PM |
Cant DataStores only be accessed by serverscripts?
~davisky~ |
|
|
| Report Abuse |
|
|
| |
|
Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 10 Feb 2016 01:41 PM |
| what do you mean serverscripts? |
|
|
| Report Abuse |
|
|
| |
|
Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 10 Feb 2016 03:20 PM |
| its letting me access them via local script on studio? |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2016 03:35 PM |
Good start. Here's some notes:
* :WaitForDataReady() was for the older DataPersistence (as far as I know). * Don't wait when a player joins/leaves, it's wasting time for no reason. * Don't use :FindFirstChild() when a player joins, use :WaitForChild(). * If you're going to use :FindFirstChild(), remember to do: Something = workspace:FindFirstChild("Something") if Something then --Code end * Include "game.OnClose = function() wait(10) end" so you have enough time to save in some cases. * Keep a server-copy of all the leaderstats because when the player object is gone, so are the leaderstats - making it hard/impossible to save them.
Enjoying your stay at the Scripters Forum? Join this! http://www.roblox.com/My/Groups.aspx?gid=2582784 |
|
|
| Report Abuse |
|
|
Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 10 Feb 2016 04:06 PM |
I have reworked my system in order so that it loads the players data in when they join and they save by clicking on a button in their starter gui.
======Load in script====== regular script in workspace====== local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats") print("cash datastore created")
game.Players.PlayerAdded:connect(function(newPlayer) print("EnteredGame") --newPlayer:WaitForDataReady() wait(5) print("data ready") local stats = newPlayer:FindFirstChild("leaderstats") stats.Cash.Value = datastore:GetAsync(newPlayer.userId .. "Cash") print("Loaded stats") wait(1) datastore:SetAsync(newPlayer.userId .. "Cash", stats.Cash.Value) print("Saved stats") end)
=======Save script ======= Local Script in button in the players gui ==== local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats") local msg = script.Parent.Parent.Label:Clone() print("cash datastore created")
local player = game.Players.LocalPlayer local leaderstats = player:WaitForChild("leaderstats")
wait(7)
function saveStats() print("clickedS") local message = msg:Clone() message.Text = "Saving . . ." message.Parent = script.Parent.Parent datastore:SetAsync(player.userId .. "Cash", leaderstats.Cash.Value) print("Saved stats") end
script.Parent.MouseButton1Down:connect(saveStats) |
|
|
| Report Abuse |
|
|
Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 11 Feb 2016 09:04 AM |
| So does datastore in a local script only work in studio testing? Will i have to rework everything as regular scripts? |
|
|
| Report Abuse |
|
|
Raildex
|
  |
| Joined: 06 Dec 2009 |
| Total Posts: 934 |
|
|
| 11 Feb 2016 09:17 AM |
Will this work? (I cannot play the game to test it on my wifi, only studio works) I tried testing it in studio and the function for when the player leaves got to "data ready" and then cut off.
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats") print("cash datastore created")
game.Players.PlayerAdded:connect(function(newPlayer) print("EnteredGame") --newPlayer:WaitForDataReady() --wait(5) print("data ready") local stats2 = newPlayer:WaitForChild("leaderstats") stats2.Cash.Value = datastore:GetAsync(newPlayer.userId .. "Cash") print("Loaded stats") wait(1) datastore:SetAsync(newPlayer.userId .. "Cash", stats2.Cash.Value) print("Saved stats") end)
game.Players.PlayerRemoving:connect(function(player) print("ExitedGame") --player:WaitForDataReady() print("data ready") --wait(2) local stats = player:WaitForChild("leaderstats") datastore:SetAsync("Cash", stats.Cash.Value) print("Saved stats") end)
game.OnClose = function() wait(10) end |
|
|
| Report Abuse |
|
|