I made a script to save and get data that isn’t working now.
I tried to find the error, but I found nothing.
Script here:
local DataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")
local plrService = game:GetService("Players")
function GetPlrData(player)
wait()
local PlrKey = player.UserId
local SaveValue = player.plrstats.Coins
local GetData = DataStore:GetAsync(PlrKey)
local success, err = pcall(function()
if GetData then
SaveValue.Value = GetData
end
end)
if success then
print("Got Data Successfully")
elseif err then
print("Error on Get Data")
end
end
function SavePlrData(plr)
local success, err = pcall(function()
DataStore:SetAsync(plr.UserId, plr.plrstats.Coins.Value)
end)
if success then
print("Saved Successfully")
elseif err then
print("Error on Save")
end
end
plrService.PlayerAdded:connect(GetPlrData)
plrService.PlayerRemoving:connect(SavePlrData)
(Sorry if I have a grammar error, I don’t speak english too good)
pcall is for code that has a chance of erroring, such as networked calls to roblox API’s (any method suffixed with Async). pcall returns a boolean indicating if the code ran successfully, or if it errored. If that boolean is false then the next thing returned by pcall is the error message as a string.
The way you’re currently using pcall will essentially silence the errors, only telling you when one has occurred, instead of telling you why one has occurred.
Here’s the logic in GetPlrData reworked to properly pcall the risky method (GetAsync), as well as output any errors if they happen to occur.
local GetData
local success, err = pcall(function()
GetData = DataStore:GetAsync(PlrKey)
end)
if success then
print("Got Data Successfully")
SaveValue.Value = GetData
else
warn(err)
end
There is a large chance that this is an actual error that was reported to roblox and fixed, but they will not publish the fixes for deprecated methods. Changing this might just fix the error. That is just a possibility and is worth trying. :Connect() is better performance-wise anyways.
local GetData
local success, err = pcall(function()
GetData = DataStore:GetAsync(PlrKey)
end)
if success then
print("Got Data Successfully")
SaveValue.Value = GetData
else
warn(err)
end
GetAsync returns nil if no data exists for that key. PlayerRemoving doesn’t have enough time to save in studio test so you need to test ingame, or use a local test with 2 clients, and only close one of them.
Try the local host 2 clients test. Make sure you fix the pcall in SavePlrData then watch the server output when you close a client. If you haven’t changed Coin’s value it will still be nil from the initial load so handle that with a default value.
You have no idea what it’s even doing until you have 2 clients to keep one ingame and read the output. Unless you’re going ingame with 2 clients and watching the developer console, nothing can be ascertained outside of it doesn’t work. You need to debug it and identify why it doesn’t work by watching where its logging.