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

I am getting these errors


oh yes, right. wait

This text will be hidden

try doing this,

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()
        
        local GetSaved = 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)


V is still unknown

oh lmao, yes again wait let me fix it, i forgot to change it to player, sorry lol.

1 Like

okay, try this

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()
        
        local GetSaved = 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, {player.leaderstats.Coins.Value, player.leaderstats.Money.Value, player.leaderstats.Gems.Value, player.leaderstats["Eggs Opened"].Value, player.leaderstats["Snow Coins"].Value, player.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)

still… It doesn’t seem to be saving in studio…

oh, but does the print message shows up?

Yep, everything shows up perfectly, it also doesn’t even seem to save data in the normal servers (Roblox)

oh, wait

This text will be hidden

can you try this and see if it prints ‘hello’, i think it may be saving but i messed up the loading (or not, 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()
        
        local GetSaved = 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]
            print("hello")
    	else
	    	local NumberforSaving = {save1.Value, save2.Value, save3.Value, save4.Value, 
            save5.Value, save6.Value}
	    	ds:GetAsync(plrkey, NumberforSaving)
            print("hello")
	    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, {player.leaderstats.Coins.Value, player.leaderstats.Money.Value, player.leaderstats.Gems.Value, player.leaderstats["Eggs Opened"].Value, player.leaderstats["Snow Coins"].Value, player.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)

Yep


Still doesn’t seem to save in studio, I haven’t tried in-game (main servers, roblox) yet

is this else are for the starter players?

What do you mean?

This text will be hidden

i mean like default value when a player joined the first time

The default value is 0, and when they leave it saves whatever stats they got

oh, okay. can you try it ingame?

edit: i will try to recreate the values and script to see if i can fix it, wait

Yea, ofcourse give me a second

Summary

This text will be hidden

Nope, it doesn’t seem to save in-game either

(and yes it gives the outputs)
I have to go to bed, so I can not respond till tomorrow if you do respond

Doesn’t change anything. Just a datastorename