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)

3 comments
i am sorry if it doesnt work, im kinda new to tables
ok i will try it
i changed it and it did not do anything