Datastore NOT working

Hi, I have two systems I need finished quick. They are being held up because of data stores. I’m having trouble getting data to save. Why isn’t my data store working? In both of the systems there are no errors, but the data just doesn’t save. Yes, I have the API setting enabled, can someone help me?

hmm, show some of the code and i’ll see if i can spot the issue

1 Like

I don’t want to show all the code, because it’s for someone else’s game.

I can tell you that there are no errors, and I’ve tried printing and I’m not getting any results.

Here’s all I will show.

P.PlayerRemoving:Connect(function(player)
	local suc, err = pcall(function()
		xpStore:SetAsync(player.UserId, player.leaderstats.XP.Value)
	end)

	if not suc then
		warn("DATA WAS NOT SAVED! ", player.UserId)
	else
		print("successfully?")
	end
end)

nothing prints.

1 Like

What is P?

1 Like

Of course, it’s game.Players. I have to add this here due to the character count min limit.

1 Like

maybe ensure that the script is in ServerScriptService and that the script is enabled? weirdly this code is working fine in a local project

This is not really the problem, other code in the script is already running.

are there any loops in the code? i find sometimes having while loops can make part of the script not function correctly

hard to really diagnose without seeing a better snippet of the code since this is pretty basic datastore stuff shown

No, nothing yielding, it’s all just events or variable declarations.

I printed a statement directly before the code snippet I sent you, so it’s not a problem.

weird, not sure i really know what to look into from here, sorry man :frowning: bit hard to tell with just that snippet since its pretty standard stuff you do for datastores, maybe try running the datastore in a modulescript and requiring it where its needed?

This is flustrating, I’m not getting any errors. Sure there isn’t anything else? I’ve been having this problem for a day now.

as i mentioned above, you could probably make a modulescript with commands for editing/setting the datastore, or you could try use a module like DataStore2 which i use on most of my projects

its a great plugin that avoids data loss and other issues with datastores :slight_smile: which could help out with whatever is messing with you here haha

Could you try putting that code in a whole new script and telling me what happens?

I already stated, that I tried printing before the code snippet, and there was no problem, it printed fine, so it can’t be a problem.

Other code in the same script is also working fine.

Please just try it. It’s the only thing I can do because as you said earlier:

2 Likes
local P = game:GetService("Players")
local DSS = game:GetService("DataStoreService")

local xpStore = DSS:GetDataStore("XP")

P.PlayerRemoving:Connect(function(player)
	local suc, err = pcall(function()
		xpStore:SetAsync(player.UserId, player.leaderstats.XP.Value)
	end)

	if not suc then
		warn("DATA WAS NOT SAVED! ", player.UserId)
	else
		print("successfully?")
	end
end)

Here’s the code I’m using.

Nothing prints when I leave.

Put this code in a whole new script in ServerScriptService and tell me what it prints:

local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")

local xpStore = DSS:GetDataStore("XP")

Players.PlayerRemoving:Connect(function(player)
	print(player.Name)
	local success, errorMessage = pcall(function()
		xpStore:SetAsync(player.UserId, player.leaderstats.XP.Value)
	end)

	if not success then
		warn("DATA WAS NOT SAVED!", player.UserId)
		warn(errorMessage)
	else
		print("successfully?")
	end
end)
1 Like

It’s printing my name, nice.