I'm having problems with Data Store

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)

1 Like

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
2 Likes

Ok, thanks for the help with pcall.
But the main problem isn’t solved.

1 Like

Try changing the connects to Connects.

This shouldn’t be the problem since lowercased c in Connect only means it is deprecated but it works fine.

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.

No, this isn’t the error, the print() it’s working with the “connect()”.

It is still a recommended fix.

BTW, try

print(err)

here:

This is already fixed in the script.

Print the value of err. It does not print the specific error in the current script.

It was solved previously by @InfinityDesign

Here:

	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

But anyway it didn’t find any error.

2 Likes

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.

1 Like

I tested ingame too, and didn’t work.

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.

SaveValue.Value = GetData or 50

I made a script to change the Coin’s value, I change the value to test, and never work.

Did you run the local test with 2 clients and verify the save print is outputting?

I don’t know how to open 2 clients, but I tested ingame, and didn’t work.

Test tab, select 2 players on drop down menu then click
image
to start the local test.

I’m having problems doing this, but I think if it didn’t work ingame, it’ll not work with the 2 clients.

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.