Data store code not working despite me copying it from another project where it works

hey, I am making a game that tracks your donations with a data store. However, when I try to save it, it doesnt work. I was able to pinpoint the issue down to a line but cant figure out the issue with it. here is my code:

local DSS = game:GetService("DataStoreService")

local spentstore = DSS:GetDataStore("spentstore")


game.Players.PlayerAdded:Connect(function(player)
	local data = Instance.new("Folder")
	data.Name = "leaderstats" 
	data.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Donations"
	cash.Parent = data
	
	local loadspent                                                   
	local success, errormessage = pcall(function()
		loadspent = spentstore:GetAsync(player.UserId.." spent")
	end)

	if success then
		cash.Value = loadspent
	else 
		print("error while recieving data")
		warn(errormessage)                                              
		player:Kick("error loading data, please try again")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	print("the gaming fungus")
	local success, errormessage = pcall(function()
		spentstore:SetAsync(player.UserId.." spent",player.leaderstats.Donations.Value)
		print("the gaming fungus")
	end)

	if success then
		print("spent succesfully saved!")
	else
		print("spent saving failed!")
		warn(errormessage)
	end
end)

“the gaming fungus” only prints out once, so I can assume that the issue is with the SetAsync function. Any help?

Just to make sure, are you testing it in an actual game? Datastores do not work in Studio (EDIT: unless you have API access enabled like @colbert2677 mentioned)

Correction: they do not work in Studio unless you have API access enabled.

yes, I do have API access on. I should have clarified that the game is only having issues SAVING the store and not loading it

Any errors perhaps what isn’t printing?

Change this to

local loadspent = nil

Also, don’t ask me why it doesnt work that way. I don’t know either, lol.

this does not seem to work. im pretty sure setting it to nil i what i was alr doing anyway

that’s normally a problem with getting data for some reason. I’m not too good with pcalls and honestly have no idea how to help there. Sorry.

Ok, I tested this script out and it didn’t work until I added a Bindtoclose function for some reason (might be because studio only works with BindToClose). It also started to print out the PlayerRemoving correctly.

local DSS = game:GetService("DataStoreService")

local spentstore = DSS:GetDataStore("spentstore")


game.Players.PlayerAdded:Connect(function(player)
	local data = Instance.new("Folder")
	data.Name = "leaderstats" 
	data.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Donations"
	cash.Parent = data
	
	local loadspent = nil                                                 
	local success, errormessage = pcall(function()
		loadspent = spentstore:GetAsync(player.UserId.." spent")
	end)

	if success then
		cash.Value = loadspent
	else 
		print("error while recieving data")
		warn(errormessage)                                              
		player:Kick("error loading data, please try again")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	print("the gaming fungus")
	local success, errormessage = pcall(function()
		spentstore:SetAsync(player.UserId.." spent",player.leaderstats.Donations.Value)
		print("the gaming fungus")
	end)

	if success then
		print("spent succesfully saved!")
	else
		print("spent saving failed!")
		warn(errormessage)
	end
end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		print("the gaming fungus")
		local success, errormessage = pcall(function()
			spentstore:SetAsync(v.UserId.." spent",v.leaderstats.Donations.Value)
			print("the gaming fungus")
		end)

		if success then
			print("spent succesfully saved!")
		else
			print("spent saving failed!")
			warn(errormessage)
		end
	end
end)

Try taking the SetAsync line out of the pcall and see if it throws an error

I added a print before the async, and it printed, so the pcall is working

I copied this script and it still didnt work. It might be an issue with my studio then

ignore my last reply! I accidentally didnt copy the bindtoclose LOL. I recopied it and it worked

The SetAsync call may be erroring and because it’s inside a pcall, it isn’t being logged.

I suggest you take a look at the issue more instead of copying the other person’s response, this issue likely will return in the future.