Data storage won't save and i don't know why

essentially whenever I enable “roundabouts” value in game it doesn’t save and goes back to disabled when i join back and I’m too new to scripting to understand what I’m doing wrong

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local DataBase = DataStoreService:GetDataStore("SwordStuff")
local SessionData = {}

function PlayerJoin(player)
	print(player, "joined")
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "SwordFolder"
	leaderstats.Parent = player
	
	
	local LocusDiveUnlocked = Instance.new("BoolValue")
	LocusDiveUnlocked.Name = "LocusDiveUnlocked"
	LocusDiveUnlocked.Parent = leaderstats
	
	local RoundaboutUnlocked = Instance.new("BoolValue")
	RoundaboutUnlocked.Name = "RoundaboutUnlocked"
	RoundaboutUnlocked.Parent = leaderstats
	
	local BringDownTheHammerUnlocked = Instance.new("BoolValue")
	BringDownTheHammerUnlocked.Name = "BringDownTheHammerUnlocked"
	BringDownTheHammerUnlocked.Parent = leaderstats
	
	local TwisterUnlocked = Instance.new("BoolValue")
	TwisterUnlocked.Name = "TwisterUnlocked"
	TwisterUnlocked.Parent = leaderstats
	
	local success = nil
	local playerdata = nil
	local attempt = 1
	
	repeat
		success, playerdata = pcall(function()
			return DataBase:GetAsync(player.UserId)
		end)
		
		attempt += 1
		
		if not success then
			warn(playerdata)
			task.wait(3)
		end
	until success or attempt == 5
	
	if success then
		print("Connected to database")
		if not playerdata then
			print("assigning default data")
			playerdata = {
				["locusDiveUnlocked"] = false,
				["roundaboutUnlocked"] = false,
				["bringDownTheHammerUnlocked"] = false,
				["twisterUnlocked"] = false
			}
		end
		SessionData[player.UserId] = playerdata
	else
		warn("Couldn't get data for", player.UserId)
		player:kick("Game couldn't get data for you. leave and come back")
	end
	
	RoundaboutUnlocked.Value = SessionData[player.UserId].roundaboutUnlocked
	
	RoundaboutUnlocked.Changed:Connect(function()
		SessionData[player.UserId].roundaboutUnlocked = RoundaboutUnlocked.Value
	end)
	
	leaderstats.Parent = player
end

players.PlayerAdded:Connect(PlayerJoin)

function playerleaving(player)
	if SessionData[player.UserId] then
		local success = nil
		local errormsg = nil
		local attempt = 1
		
		repeat
			success, errormsg = pcall(function()
				DataBase:SetAsync(player.UserId, SessionData[player.UserId])
			end)
			
			attempt += 1
			print(player, "left")
			
			if not success then
				warn(errormsg)
				task.wait(3)
			end
		until success or attempt == 5
		
		if success then
			print("Data saved for", player.Name)
		else
			warn("couldn't save data for", player.Name)
		end
		
	end
end

players.PlayerRemoving:Connect(playerleaving)
3 Likes

It could just be the fact that you have the RoundaboutUnlocked.Changed function in the PlayerJoin function, which means that after the PlayerJoin function ends (after parenting leaderstats to the player) the script no longer has access to the RoundaboutUnlocked.Changed function.

I’m not entirely sure if this is the case, so before changing anything major, I recommend putting a print statement in the RoundaboutUnlocked.Changed function just to check if it’s running at all.

I don’t think i can do that because it refers to the “roundaboutUnlocked” boolvalue in the playeradded section(then again maybe there is a way and i just don’t know

1 Like

I’ve tried the script myself and it worked perfectly fine, are you sure there isn’t an issue elsewhere?

Try printing SessionData[player.UserId].roundaboutUnlocked before you set the value and add a print to the changed function.

After all of that try again and see what gets printed to output.

1 Like

did you write the print statement to make sure it’s running in the first place? it’d be after the RoundaboutUnlocked.Changed:Connect(function()

yea its under that and it still won’t work

ok but does it print what’s inside the statement or not? because if not that means it isn’t running at all.

1 Like

it doesn’t print in the output at all

So it’s not that your data store isn’t saving properly, it’s that your session data is not being changed at all. If I were you, instead of using a value object in the workspace, I would use a module to store the session data and modify the session data directly so you don’t have to deal with a weird middleman connection here. I’ll link you a response that helped me: