DataStore saving only in Studio not in game

I have the basic shape of script of dataStore to save values.
In studio this works 100%! Saving and loading all the changes i’ve made.

BUT when i go to the real game (client) and enter the first time, all values are loaded correctly (nice), but when i close the client, all the new data SHOULD have been saved but this is not the case. And i can’t see the Console (F9) because i quit the game.
Anyone experienced this before?
Any tips?

Can you show your code?

Can you please show your code?

Loading:

game.Players.PlayerAdded:Connect(function(player)
local Variaveis = Instance.new("Folder")
	Variaveis.Name = "Variaveis"
	Variaveis.Parent = player	

	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Value = 0
	Money.Parent = Variaveis


local data
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)
if success and data ~= nil then
if data[2] ~= nil and data[2] ~= 0 then
			Money.Value = data[2]
		end
end
// and so on...

Saving data: This is Modules script.

local tableToSave = {player.Variaveis.Money.Value;}
for i=1, 3 do
		local success, err = pcall(function()
			dataStore:SetAsync(player.UserId, tableToSave)
		end)
		if success then
			print("Data foi salvo! De:"..player.Name)
			break
		else
			if i>1 then print("Tentativa "..i.." de salvar as informações do Jogador:"..player.Name.."/"..player.UserId) end
			if i == 3 then warn("Data NÃO FOI SALVA! Jogador:"..player.Name.."/"..player.UserId) end
			task.wait(2)
			warn(err)
		end
	end

Player Leaving code:

local SSS = game:GetService("ServerScriptService")
local Game_Configs = require(SSS:WaitForChild("Game Config"))

local Save_Load = require(script.Parent:WaitForChild("Save_Load"))
local jogadores_salvos = {}
local Players = game:GetService("Players")

local function saveData(player:Player)	
	if player:FindFirstChild("NAO_SALVAR") ~= nil then
		if player.NAO_SALVAR.Value == true then
			return warn("Não vai salvar o jogador:"..player.Name)
		end
	end
	Save_Load.SaveData(player)
end

local function VerifyIfSaved(player)
	for i , id in pairs(jogadores_salvos) do
		if id == player.UserId then			
			print(player.name.." Já foi salvo anteriormente")
			return true
		end
	end
	table.insert(jogadores_salvos,player.UserId)
	--print("Colocando player na tabela para salvar")
	return false
end

game:BindToClose(function()
	if Game_Configs["Save E Load"]["Salvar Contas"] == true then
		print("Salvando informaçoes dos Jogadores")
		for _, player in pairs(Players:GetChildren()) do
			local verificar = VerifyIfSaved(player)
			if verificar == false then
				saveData(player) 
				task.wait(1)
			end			
		end
		print("Salvou com SUCESSO as infos dos Jogadores")
		task.wait(6)
	end	
end)


-- Quando o jogador desconecta do servidor:
game.Players.PlayerRemoving:Connect(function(player)
	print("Player Leaving:"..player.Name)
	if Game_Configs["Save E Load"]["Salvar Contas"] == true then
		local verificar = VerifyIfSaved(player)
		if verificar == false then
			saveData(player)
		end
		task.wait(1)
		table.remove(jogadores_salvos, table.find(jogadores_salvos, player))
	end	
end)

Not sure why your saving is over three scripts, but you have to save when the game restarts too.

You can do this with :BindToClose.

Edit: Nevermind, just realized you have it implemented already.

Because the data is big. Only the loading script is 330 lines… i wrote a part of it for you.

Try this.

– Loading


game.Players.PlayerAdded:Connect(function(player)
    local Variaveis = Instance.new("Folder")
    Variaveis.Name = "Variaveis"
    Variaveis.Parent = player

    local Money = Instance.new("IntValue")
    Money.Name = "Money"
    Money.Value = 0
    Money.Parent = Variaveis

    local success, data = pcall(function()
        return dataStore:GetAsync(player.UserId)
    end)

    if success and data then
        if type(data) == "table" and #data >= 1 then
            player.Variaveis.Money.Value = data[1] or 0
        else
            warn("Invalid data for player: " .. player.Name)
        end
    end
end)

–saving data (module script)

local tableToSave = {
    player.Variaveis.Money.Value,
}

local success, err = pcall(function()
    dataStore:SetAsync(player.UserId, tableToSave)
end)

–player leaving code

local function saveData(player)
    if player:FindFirstChild("NAO_SALVAR") ~= nil then
        if player.NAO_SALVAR.Value == true then
            return warn("Não vai salvar o jogador:"..player.Name)
        end
    end
    Save_Load.SaveData(player)
end

game:BindToClose(function()
    if Game_Configs["Save E Load"]["Salvar Contas"] == true then
        print("Salvando informaçoes dos Jogadores")
        for _, player in pairs(Players:GetPlayers()) do
            local verificar = VerifyIfSaved(player)
            if verificar == false then
                saveData(player)
                task.wait(1)
            end
        end
        print("Salvou com SUCESSO as infos dos Jogadores")
        task.wait(6)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    print("Player Leaving:"..player.Name)
    if Game_Configs["Save E Load"]["Salvar Contas"] == true then
        local verificar = VerifyIfSaved(player)
        if verificar == false then
            saveData(player)
        end
        task.wait(1)
        table.remove(jogadores_salvos, table.find(jogadores_salvos, player.UserId))
    end
end)

Remembering that all the scripts WORKS FINE in studio!

Are you testing using Local Server in studio?

No. Only running (F5) Play button

Can you please look what i said.

can you mark what you have changed for me ?

I have changed the upper half but i now see i should finished the playeradded function i also changed the table in the module script to be a lot shorter and more efficient

I have fixed it now. You can try the scripts

I found the problem.
The script that gets the itens of the player is a server script but its trying to search itens inside PlayerGui. But when player closes the client, the playerGui is gone!