Datastore isn't working

*Achieve: Trying get my datastore to save & load.
*Issue: It won’t save or load at all.
*Solution: I am new to using datastores and i’ve tried to follow 2 different tutorials and i read through a post on here and it still isn’t working | API service is on btw.

All help is appreciated, Thank you.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("StatsSave")

local DefaultCash =
{Cash = 0;}

-- Opening Data and Adding Data

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local data
	local sucess, errormessage = pcall(function()
	data = DataStore:GetAsync(player.UserId.."-cash")
	end)
		
		if sucess then
			cash.Value = data
		else
			print("There was an error whilst getting your data")
			warn(errormessage)
		end
end)


-- Save Data

game:GetService("Players").PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
	DataStore:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
	end)
		
		if success then
			print("DataSaved")
	else
			print("Error with saving")
			warn(errormessage)
		end
end)

One this I noticed first is that you’re saving it with key player.UserId.."-Cash" and trying to load it in using key player.UserId.."-cash" - those are two different keys so that might be your issue.

1 Like

Thanks for the fast reply, I am honestly so dumb sometimes. It works now :slight_smile:

1 Like