Problem with saving data

So, i was watching a youtube video to save data. And it had rebirths in it so i removed it, and it’s not working, can you help me? Link to youtube video: Saving Data - How to make a simulator in ROBLOX Part 5 - YouTube and script:

local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
game.Players.PlayedAdded:Connect(function(plr)
	wait()
	local plrkey = "Id_"..plr.userid
	local savevalue = plr.leaderstats.Clicks

local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
savevalue.Value = GetSaved(1)
	else
		local NumbersForSaving = {savevalue.Value}
		DS:GetAsync(plrkey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id_"..plr.UserId, {plr.leaderstats.Clicks.Value})
end)

What doesn’t work in your script? Any errors?

Yes, only one.
PlayedAdded is not a valid member of Players “Players”

PlayedAdded? Do you mean… PlayerAdded? :joy:

Don’t forget to mark for a solution if I helped. :smiley:

1 Like

Fixed grammar error, and still doesnt work.

Any output errors you got this time?

None. Tried ingame, doesnt work.

That would only mean GetSaved is failing. Seeing you have

local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
1 Like

Tbh, that tutorial isn’t good. I’d recommend this one. It’s how I learned how to use data stores:

Is it long? Yes, but he goes through basically everything u need to make a data store.

Yeah, that’s why you should use pcalls to see what’s wrong.

Ok heres the fix I guess.
Also make sure that studio access to api services is enabled.

local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "Id_"..plr.userId
	local savevalue = plr.leaderstats.Clicks
local Success, ErorrMsg = pcall(function()
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		savevalue.Value = GetSaved
	else
		local NumbersForSaving = savevalue.Value
		DS:GetAsync(plrkey, NumbersForSaving)
	end

	end)
	if Success then
		print("Success loading data")
	else
		warn(ErorrMsg)
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("Id_"..plr.UserId, plr.leaderstats.Clicks.Value)
end)

Proof it works:
Before:

image

after:

And I’m still stuck with a probably basic problem on my own topic :sweat_smile:

Hopefully, I could help though!

Try testing it in Roblox player. I heard somewhere that PlayerRemoving doesn’t always run in studio.

This didn’t work. Look at the post the post I did above. That one works.

Turns out you messed up, too. It should be plr.UserId, not plr.userid.

1 Like

Yeah, i fixxed it already. :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

And can you help me with having the same value in my counter the same as leaderstats? Thank you.