Why does my Leaderstats Datastore only save sometimes in Roblox Studio, but all the time in Roblox?

Thats a normal thing for some reason

Okay well, I just tried it, and I don’t really see it saving it either in Roblox Studio? It still does the same thing it did before, and the print does come out saying it was successful

Make sure you also have the playerRemoving function in your script as well as the bindtoclose.

in here you still added userId which i’ve already did here

try removing the userId ang just do it like

savedata:SetAsync("id_"..id

So like this?

			savedata:SetAsync("id_"..id, {id.leaderstats.Coins.Value, id.leaderstats.Money.Value, id.leaderstats.Gems.Value, id.leaderstats["Eggs Opened"].Value, id.leaderstats["Snow Coins"].Value, id.leaderstats["Snow Cash"].Value}) -- change data to the value you want to save

yes, that is right again.

asdasdasd

It still seems to have the same outcome

wait what is this, why’d you do id.leaderstats, it should be plr (or ‘v’ since it’s in a for loop) right? maybe it’s because of this, sorry i didn’t see this earlier

Because I was getting this and I assumed I had to put ID because you put ID in the beginning

don’t put plr but instead use ‘v’ because that’s what i named the player variable

Still seems to do it

local ds = game:GetService("DataStoreService"):GetDataStore("savedata")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Coins
	local save2 = plr.leaderstats.Money
	local save3 = plr.leaderstats.Gems
	local save4 = plr.leaderstats["Eggs Opened"]
	local save5 = plr.leaderstats["Snow Coins"]
	local save6 = plr.leaderstats["Snow Cash"]

	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
		save3.Value = GetSaved[3]
		save4.Value = GetSaved[4]
		save5.Value = GetSaved[5]
		save6.Value = GetSaved[6]
	else
		local NumberforSaving = {save1.Value, save2.Value, save3.Value, save4.Value, save5.Value, save6.Value}
		ds:GetAsync(plrkey, NumberforSaving)
	end
end)

local datastoreservice = game:GetService("DataStoreService")
local savedata = datastoreservice:GetDataStore("savedata") -- your data store ofc

game:BindToClose(function()
	local players = game.Players
	local s, errorms = pcall(function()    
		for _, v in pairs(players:GetChildren()) do
			local id = v.UserId
			savedata:SetAsync("id_"..id {v.leaderstats.Coins.Value, v.leaderstats.Money.Value, v.leaderstats.Gems.Value, v.leaderstats["Eggs Opened"].Value, v.leaderstats["Snow Coins"].Value, v.leaderstats["Snow Cash"].Value}) -- change data to the value you want to save
		end
	end)

	if s then
		print("success saving players data")
	else
		warn(errorms)
	end
end)

can i see the playeradded script which loads the data?

What do you mean? That script does it

  • List item
    tessssss

oh yes, right im sorry. i wasn’t reading i thought it was a separate script, lol.

No, you’re all good

asdasdasdasdasdasd

i noticed that the getasync is not in a pcall maybe try adding a pcall?
edit: also where’s the player removing? bindtoclose only functions when a server is dying so other players data would not get saved

Where exactly do I put the pcall? And is this what you mean?

local s, e = pcall(function()
    -- save
end)

if not s then
    print(asdasdasdasdwasdwasd) -- prints the error
end

yes, right.

This text will be hidden

So where do I put the pcall?

Summary

adsdasdasdwasdwasdwasdwasdwasdwasdwasdwasdwasd

i edited the script and it should work now, i hope lol.

local ds = game:GetService("DataStoreService"):GetDataStore("savedata")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Coins
	local save2 = plr.leaderstats.Money
	local save3 = plr.leaderstats.Gems
	local save4 = plr.leaderstats["Eggs Opened"]
	local save5 = plr.leaderstats["Snow Coins"]
	local save6 = plr.leaderstats["Snow Cash"]

	local s, e = pcall(function()
        
        ds:GetAsync(plr.UserId)
   
	    if GetSaved then
	    	save1.Value = GetSaved[1]
	    	save2.Value = GetSaved[2]
	    	save3.Value = GetSaved[3]
		    save4.Value = GetSaved[4]
		    save5.Value = GetSaved[5]
	    	save6.Value = GetSaved[6]
    	else
	    	local NumberforSaving = {save1.Value, save2.Value, save3.Value, save4.Value, 
            save5.Value, save6.Value}
	    	ds:GetAsync(plrkey, NumberforSaving)
	    end
    end)

    if s then
        print("succefully loading player data")
    else
        warn("could not load player's data")
    end
 end)

game.Players.PlayerRemoving:Connect(function(player)
    local s, e = pcall(function()
        ds:SetAsync("id_"..player.UserId, {v.leaderstats.Coins.Value, v.leaderstats.Money.Value, v.leaderstats.Gems.Value, v.leaderstats["Eggs Opened"].Value, v.leaderstats["Snow Coins"].Value, v.leaderstats["Snow Cash"].Value})
    end)

    if not s then
        warn("Could not save "..player.Name.."'s data!")
    end
end)

game:BindToClose(function()
	local players = game.Players
	local s, errorms = pcall(function()    
		for _, v in pairs(players:GetChildren()) do
			local id = v.UserId
			ds:SetAsync("id_"..id, {v.leaderstats.Coins.Value, v.leaderstats.Money.Value, v.leaderstats.Gems.Value, v.leaderstats["Eggs Opened"].Value, v.leaderstats["Snow Coins"].Value, v.leaderstats["Snow Cash"].Value}) -- change data to the value you want to save
		end
	end)

	if s then
		print("success saving players data")
	else
		warn(errorms)
	end
end)

edit: i did a typo, it’s fixed now