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 » Scripters
Home Search
 

Re: does my datastore script look ok?

Previous Thread :: Next Thread 
Raildex is not online. 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 is not online. 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 is not online. 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 is not online. davisky2
Joined: 04 Mar 2012
Total Posts: 4710
10 Feb 2016 11:17 AM
I would better suggest an autosave.


~davisky~
Report Abuse
Raildex is not online. 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 is not online. 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
cheesecake123456 is not online. cheesecake123456
Joined: 01 Jun 2009
Total Posts: 1529
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 is not online. 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 is not online. davisky2
Joined: 04 Mar 2012
Total Posts: 4710
10 Feb 2016 12:48 PM
Cant DataStores only be accessed by serverscripts?


~davisky~
Report Abuse
cheesecake123456 is not online. cheesecake123456
Joined: 01 Jun 2009
Total Posts: 1529
10 Feb 2016 12:52 PM
Yes, it says on the wiki


Report Abuse
Raildex is not online. Raildex
Joined: 06 Dec 2009
Total Posts: 934
10 Feb 2016 01:41 PM
what do you mean serverscripts?
Report Abuse
cheesecake123456 is not online. cheesecake123456
Joined: 01 Jun 2009
Total Posts: 1529
10 Feb 2016 01:43 PM
Just normal scripts.*


Report Abuse
Raildex is not online. 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
powerhotmail123 is not online. powerhotmail123
Joined: 11 Apr 2011
Total Posts: 5041
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 is not online. 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 is not online. 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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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