Autosave script doesn't work

Hello, totday i tried to make a game with an currency that autosaves but it didn’t work.
I looked to the Youtube video from AlvinBlox that’s named Roblox DataStore Tutorial Data Stores & Saving Data Scripting Tutorial. I put the script under this text and I rereaded it to much but i can’t find a mistake.

can the script maybe be outdated?

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

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 success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:GetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
	end)
	
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
	
end)

btw, i turned API services on

Datastores don’t work the greatest in studio so I recommend testing it in the actual game first.

I did tested it in the real game like AlvinBlox did to

maybe is it because the game is private or does that not matter?

1 Like

Like whale said, it’s not best to test it in studio since when you click the stop/leave button then the studio session will automatically close without saving any data

1 Like

I don’t think it matters if your game is private or not

1 Like

No, that doesn’t matter. Did you publish the game before testing in the actual game?

Yes, i did it around 5 times. then wait a few minutes and played. then i left and waited a minute again and rejoined. but it still doesn’t save

1 Like

Do you get any output when leaving the game? (press F9 and go to server to look at the output)

1 Like

Yup, I tried your exact script and it didn’t work. It may be dated like you said. Try this tutorial by TheDevKing instead, I’ve used this tutorial before and it works:

1 Like

Try replacing this with data = myDataStore:GetAsync(player.UserId.."-cash",data)

Nope, that still doesn’t work. i think i just try AWhale_OfATime his solution and look TheDevKing his video.
thx for helping

1 Like
game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:GetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
	end)

When the player is leaving, you need to call :SetAsync and not :GetAsync. You should change it now and it will fix your error

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
	end)

If I change it to what you said, then the Output says “There was an error whilst getting your data”