DataStore isn't Saving

If this is for your game… of course it wont work. You said if in studio the game is checking to see if you are in studio. You are not. You are in your game. So you could create an else statement to check if you are not in studio to save data.

It says that with the BindToClose() functions?

Oh lol. I didn’t see that. But what data are you returning now?

Basically tryna save player Coin, XP and Rebirth but for some reason it’s refusing to save

That’s the main problem with your code, use a dictionary to store data, it’s the easiest and most convenient method.

Do you mind showing Example please?

why are you defining the datavariable to 4 different things

But are dictionaries really necessary. I believe you can do just fine without dictionaries

Dictionaries are necessary… for so many things

Is that a bad thing?

30charrrrrrs

Yes very Bad, use a table for your SetAsync

assign all of it to a table?

30charrrrrrrss

local Data = {player.leaderstats.Coin.Value, player.leaderstats.XP.Value, player.leaderstats.Rebirth.Value}

local success,errormessage = pcall(function()
   DataStoreService:SetAsync(key,Data)
end)

build off of this

I didn’t say they were not necessary. I was just wondering if it was suitable for this case. Because I believe you can do this without dictionary.

(Not saying it wasn’t useful or any thing.

Oh sorry for the misunderstanding lol.

Here’s a way you can make a dictionary of the values you’re saving (Coin / XP / Rebirth)

  game.Players.PlayerRemoving:Connect(function(player)
        local key = "Player_" .. player.UserId
       
        local Data_Table = {} --I will name the table Data_DiningRoomTable beyond this point replace it
        Data_DiningRoomTable.Coin = player.leaderstats.Coin.Value
        Data_DiningRoomTable.XP = player.leaderstats.XP.Value
        Data_DiningRoomTable.Rebith = player.leaderstats.Rebirth.Value
           
        print('Dictionary being stored',Data_Table)

        local success, errormessage = pcall(function()
          Data = DataStoreService:SetAsync(key, Data_Table)
        end)
            if success then
              print("Success!")
            else
              print("Unsuccess!")
              warn(errormessage)
            end
    end)

Thank you @akitsuya, @Extrenious and @xKaihatsu I’ve learned how to Save Data with table but my data still seem to not save

This isn’t a dictionary.

Here’s a code sample.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Cash"
	
	local rebirths = Instance.new("IntValue", leaderstats)
	rebirths.Name = "Rebirths"
	
	local data = nil
	local success, err = pcall(function()
		data = PlayerData:GetAsync(player.UserId.."-data")
	end)
	
	if success then -- checks if grabbing data was successful
		
		if data then -- data can be nil, so this nil check is here to check if the player has data
			
			cash.Value = data.Cash -- looks into the dictionary created in the save, goes to the "Cash" key, and grabs the value of "Cash".
			rebirths.Value = data.Rebirths -- ^ (looks for the "Rebirths" key and grabs in it's value.)
			
		else -- here is where you'd assign default values for data, since data was nil, it's safe to assume this is a new player
			
			cash.Value = 100 -- these are random default values, they can be whatever you wish.
			rebirths.Value = 0
			
		end
		
	else
		warn("There was an error whilst grabbing data!")
		warn("Error:", err)
	end
	
end)

Players.PlayerRemoving:Connect(function(player)
	
	local leaderstats = player:WaitForChild("leaderstats")
	local cash = leaderstats.Cash
	local rebirths = leaderstats.Rebirths
	
	local data = {
		Cash = cash.Value, -- here we create 2 keys "Cash" and "Rebirths" to be grabbed inside of the load function
		Rebirths = rebirths.Value
	}
	
	local success, err = pcall(function()
		PlayerData:UpdateAsync(player.UserId.."-data", function()
			return data -- saves the dictionary to the player data store.
		end)
	end)
	
	if success then
		print("Data saved successfully!")
	else
		warn("There was an error whilst saving data!")
		warn("Error:", err)
	end
	
end)

Should I add a BindToClose() function?

Yeah, but make sure the save is in a coroutine so that it can save all of the data.