DataStore Problems

(Sorry for the repost, no one bothered to answer last time)

I have a system in place that uses _G to save.

_G.sessionData[player.UserId] is a table for the player that joins.

_G.sessionData[player.UserId] = {
	settings = {
		FOV = 1
	},
	endings = {
		Shoplift = false,
		Breakfast = 0
	},
	secrets = {

	}
}

^ This is how the table looks like.

local DSS = game:GetService("DataStoreService")
local RS = game:GetService("ReplicatedStorage")
local BS = game:GetService("BadgeService")
local Players = game:GetService("Players")
local RuS = game:GetService("RunService")

local movement = RS.Movement

local config = require(script.Configuration)

local settingsDS = config.settingDS
local secretsDS = config.secretsDS
local endingsDS = config.endingsDS

local dataKey = config.dataKey

local defaults = config.defaults

local function GetData(player, dataStore)
	local data
	local success, result = pcall(function()
		data = dataStore:GetAsync(dataKey..player.UserId)
	end)
	if not success then
		warn(dataStore.Name.." could not load: "..result)
	end
	return data
end

local function SaveData(player, data, dataStore)
	local success, result = pcall(function()
		dataStore:SetAsync(data, dataKey..player.UserId)
	end)
	if not success then
		warn(dataStore.Name.." could not save: "..result)
	else
		print(dataStore.Name, "saved", player.Name.."'s", "data")
	end
end

_G.sessionData = {}
Players.PlayerAdded:Connect(function(player)
	player:LoadCharacter()
	movement:FireClient(player, false)
	
	_G.sessionData[player.UserId] = {}
	local plrData = _G.sessionData[player.UserId]
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = player
	
	local dataLoaded = Instance.new("BoolValue")
	dataLoaded.Name = "DataLoaded"
	dataLoaded.Parent = player
	
	plrData.settings = GetData(player, settingsDS) or defaults.settings
	plrData.secrets = GetData(player, secretsDS) or defaults.secrets
	plrData.endings = GetData(player, endingsDS) or defaults.endings
	
	print(plrData.endings.Shoplift)
	
	dataLoaded.Value = true
	
	local date = os.date("*t")
	if (date.hour % 24) == 4 then
		BS:AwardBadge(player.UserId, 2127043783)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local plrData = _G.sessionData[player.UserId]
	
	print(plrData.endings.Shoplift)
	SaveData(player, plrData.settings, settingsDS)
	SaveData(player, plrData.secrets, secretsDS)
	SaveData(player, plrData.endings, endingsDS)
end)

^ The script that saves and gets data. It prints when it saves.

local DSS = game:GetService("DataStoreService")

local config = {
	settingDS = DSS:GetDataStore("Settings_TESTING"),
	secretsDS = DSS:GetDataStore("Secrets_TESTING"),
	endingsDS = DSS:GetDataStore("Endings_TESTING"),
	
	dataKey = "DataKey-6-",
	
	defaults = {
		settings = {
			FOV = 1
		},
		endings = {
			Shoplift = false,
			Breakfast = 0
		},
		secrets = {

		}
	}
}

return config

^ The configuration module that I use

local RS = game:GetService("ReplicatedStorage")
local BS = game:GetService("BadgeService")

local giveEnding = RS.GiveEnding

giveEnding.Event:Connect(function(player, ending, value)
	local plrData = _G.sessionData[player.UserId]
	plrData.endings[ending] = value or true
	
	BS:AwardBadge(player.UserId, 2127044890)
end)

^ The event I use for giving a player an ending.

When I get an ending, it sets the ending variable inside the player’s session data to the value provided or true. For when I join, I print the Shoplift ending variable to see if I have it (for testing purposes). When I leave, it prints the Shoplift ending variable to tell me whether I have it or not.

When I joined, it printed false.
When I left, it printed true and saved.

But when I rejoined, it printed false again. So I don’t get it, what is preventing it from saving the table? I have avoided using DataStore2 because it failed most of the time I used it, but if it is necessary I will incorporate it.

https://developer.roblox.com/en-us/api-reference/function/GlobalDataStore/SetAsync
you mixed up the parameters. it’s (key, value), not (value, key)

this might be because you threw a lot of code an info that people have no use for until they read your question, but then put your question at the bottom