DataStore isn't Saving

Hello Guys so I’m currently working on a simulator game
Link: Tailty Simulator - Roblox

And the only thing the seems to be holding me back is the DataStoreService so basically everytime I tried to save cash it always print in the output cash has been saved but everytime when I join back the cash isn’t saved and I’ve tried anything so I decided to reach out to you guys anyone mind giving me an helping hand code is below

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
	local key = "Player_" .. player.UserId
	local leaderboard = Instance.new("Folder")
	leaderboard.Name = "leaderstats"
	leaderboard.Parent = player
	local Coin = Instance.new("IntValue")
	Coin.Name = "Coin"
	Coin.Parent = leaderboard
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Parent = leaderboard
	local Rebirth = Instance.new("IntValue")
	Rebirth.Name = "Rebirth"
	Rebirth.Parent = leaderboard
	local Data
	local success, errormessage = pcall(function()
		Data = DataStoreService:GetAsync(key)
	end)
	if success then
		Coin.Value = Data
		print("Success!")
	else
		print("Unsuccess!")
		warn(errormessage)
	end
	game.Players.PlayerRemoving:Connect(function(player)
		local key = "Player_" .. player.UserId
		local Data = player.leaderstats.Coin.Value
		local Data = player.leaderstats.XP.Value
		local Data = player.leaderstats.Rebirth.Value
		local success, errormessage = pcall(function()
			Data = DataStoreService:SetAsync(key, Data)
		end)
		if success then
		print("Success!")
		else
			print("Unsuccess!")
			warn(errormessage)
		end
	end)
end)
local PlayerData = {}
game:BindToClose(function()
	if RunService:IsStudio() then
		return
	end
	print("Saving All Player Data...")
	local players = Players:GetPlayers()
	for _, player in pairs(Players) do
		local key = player.UserId
		local Data = PlayerData[key]
		if Data then
			local success, errormessage = pcall(function()
				DataStoreService:SetAsync(key, Data)
			end)
			if not success then
				warn(errormessage)
			end
		end
	end
	print("Completed saving player Data")
end)```

are you testing in studio? if so have you made sure to allow access to game services in studio within game settings?

When I’m testing the DataStoreService the game is private

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