Datastore won't work and doesn't emit errors even after using BindToClose

Hello there, I’m trying to make a datastore but it doesn’t seem to be working, I have BindtoClose, I turned on API Service, I tried it in-game, and used a test Server in Studio, went to youtube, basically tried everything. I really don’t know what to do at this point, so if anybody knows, I’m begging please help I’ve been working on this all day long.

Script:

local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("leaderstats")
local RunService = game:GetService("RunService")


game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = 'leaderstats'
	
	local Cash = Instance.new("IntValue", leaderstats)
	Cash.Name = 'Cash'
	
	local plrdata 
	local Success, errormessage = pcall(function()
		
		plrdata = Data:GetAsync(player.UserId.."-Cash")
		
		
			
	end)
	
	if Success then
		Cash.Value = plrdata		
	else
		
		print("Oh God It didn't work somthing seems wrong")
		warn(errormessage)
	end
		
	 
	
end)

function savedata(player)
	local success, errormessage = pcall(function()
		Data:GetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)


	end)

	if success then
		print("WOW It work")
	else 

		print("goodgodIt didn't work")
		warn(errormessage)
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	
	savedata(player)
	
end)

game:BindToClose(function()
	if RunService:IsStudio() then
		return
	end
	local players = game.Players:GetPlayers()
	for _, player in pairs(players) do
		savedata()
	end
end)

you are getting the same thing you got before in the code, you dont get anything new, you want to reference the cash value instead of getting it again. Then you want to use :SetAsync to save it

1 Like

Good Lord, Bro you don’t understand how much I love you right now, if it weren’t for you I probably would’ve been stuck on this for a while,

Thank you so much,

Swift.

np, feel free to message me if you got more problems

1 Like

Alright, Thanks man.

–SwiftNumbr