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)