i made a script that is supposed to save a players stage when they leave the game. for some reason the script will not work this is the script:
local players = game:GetService("Players") local dataStoreService = game:GetService("DataStoreService") local saveDataStore = dataStoreService:GetDataStore("SaveDataTest")
local function saveplrData(plr)
local succes,err = pcall (function() local saveData = {} for _,stats in pairs (plr.leaderstats:GetChildren()) do saveData[stats.Name] = stats.Value end saveDataStore:SetAsync(plr.UserId,saveData) end) if not succes then return err end
end
players.PlayerAdded:connect(function(plr)
local stats = Instance.new("Folder") stats.Name = "leaderstats" stats.Parent = plr local stage = Instance.new("IntValue") stage.Name = "Stage" stage.Parent = stats local data = saveDataStore:GetAsync(plr.UserId) if data then print(data.Stage) for _,stat in pairs (stats:GetChildren()) do stat.Value = data[stat.Name] end else print(plr.Name .. " has no data") end plr.CharecterAdded:connect(function() local humanoid,hrp = char:WaitForChild("Humanoid"),char:WaitForChild("HumanoidRootPart") wait() if humanoid and hrp then if stage.Value ~= 0 then local part = workspace.ObbyStages:FindFirstChild(Stage.Value) hrp.CFrame = part.CFrame + Vector3.new(0,1,0) end end end
end)
players.PlayerRemoving:connect(function(plr)
local err = savePlrData(plr) if err then print(err) end
end)
game:BindToClose(function()
for_,plr in pairs (players:GetPlayers()) do local err = savePlrData(plr) if err then print(err) end end wait(2)
end)
I think this is the problem, u cant insert in a table like that ill show u how
local succes,err = pcall (function() local saveData = {} for _,stats in pairs (plr.leaderstats:GetChildren()) do table.insert(SaveData, stats.Name, stats.Value) end saveDataStore:SetAsync(plr.UserId,saveData) end) if not succes then return err end