I am trying to make my simulator game save but no matter what, the data returned from GetAsync is always 0. I think it’s because no code after SetAsync is run but I’m not sure. I have searched through the devforum and the wiki for ways to fix this but none have worked. Please Help.
More Information
Save Code
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("GBux")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gbux = Instance.new("IntValue")
gbux.Name = "GiraffeBux"
gbux.Parent = leaderstats
local data
local success, errormessage = pcall(function()
print("Getting...")
local data = DataStore:GetAsync(player.UserId.."-GBux")
print("Data is "..data..".")
end)
if success then
gbux.Value = data
print("Data Loaded!")
else
error("There was an error while loading Data. The error is "..errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
print("Saving player.leaderstats.GiraffeBux.Value. The value is "..player.leaderstats.GiraffeBux.Value..".")
DataStore:SetAsync(player.UserId.."-GBux", player.leaderstats.GiraffeBux.Value)
end)
if success then
print("Data Saved!")
else
error("There was an error while saving Data. The error is "..errormessage)
end
end)
Output
Getting...
Data is 0.
Data Loaded!
...
Saving player.leaderstats.GiraffeBux.Value. The Value is __.
(Should now say Data Saved! or, if theres and error, There was an error while saving Data. The error is ___.)
This could be for a number of reasons. One, the server possibly crashed and everyone gets kicked out. Two, one lost its internet. Three, you should use
game:BindToClose(function()
save()
wait(15)
end)
but first to test this you must reset your data using the developer console ingame:
game.Players.jake31255.leaderstats.GiraffeBux.value = 150 -- change the value to 0 or any cash value. However super high values are not allowed.
I have changed the script by following other tutorials on using DataStores but it still won’t work.
Script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("GBux")
local PlayersInGame = 0
game.Players.PlayerAdded:Connect(function(player)
PlayersInGame = PlayersInGame + 1
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gbux = Instance.new("IntValue")
gbux.Name = "GiraffeBux"
gbux.Parent = leaderstats
local data
local success, errormessage = pcall(function()
print("Getting...")
local data = DataStore:GetAsync(player.UserId.."-GBux")
print("Data is "..data..".")
end)
if success then
gbux.Value = data
print("Data Loaded!")
else
error("There was an error while loading Data. The error is "..errormessage)
end
if data == nil then
gbux.Value = 0
end
end)
local BindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
print("Saving player.leaderstats.GiraffeBux.Value. The value is "..player.leaderstats.GiraffeBux.Value..".")
DataStore:SetAsync(player.UserId.."-GBux", player.leaderstats.GiraffeBux.Value)
end)
if success then
print("Data Saved!")
PlayersInGame = PlayersInGame - 1
BindableEvent:Fire()
else
error("There was an error while saving Data. The error is "..errormessage)
end
end)
game:BindToClose(function()
while PlayersInGame > 0 do
BindableEvent.Event:Wait()
end
end)
I see why, you basicaly create a new data variable inside of the pcall function.
In here:
local data
local success, errormessage = pcall(function()
print("Getting...")
local data = DataStore:GetAsync(player.UserId.."-GBux")
print("Data is "..data..".")
end)
Remove this local in the pcall function. Because what you are basically doing here, is creating a new data variable. And the old one, never gets a value. And there’s no surprise that it prints the right data, because it prints the new variable that is inside of the function, and not the old one.