Hey, i am currently working on an obby and i am trying to make it so the stage saves when you leave, but it isn’t saving. Nothing is appearing in the output and I’m not sure on what to do.
The script:
local saveStage = DataStoreService:GetDataStore("saveStageService")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Stage = Instance.new("IntValue", leaderstats)
Stage.Name = "Stage"
Stage.Value = 1
local playerUserId = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = saveStage:GetAsync(playerUserId)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Stage.Value
local success, errormessage = pcall(function()
saveStage:SetAsync(playerUserId, data)
end)
if success then
print("Saved stage")
else
warn(errormessage)
end
end)````
Well you get the “data”, but you never use it. Just do this:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Stage = Instance.new("IntValue", leaderstats)
Stage.Name = "Stage"
Stage.Value = 1
local playerUserId = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = saveStage:GetAsync(playerUserId)
end)
if data then
Stage.Value = data
end
end)
local dss = game:GetService(“DataStoreService”)
local obbyDS = dss:GetDataStore(“ObbyData1”)
local checkpoints = workspace.Checkpoints
game.Players.PlayerAdded:Connect(function(plr)
local obbyData = obbyDS:GetAsync(plr.UserId .. "-obbyStageProgress")
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
ls.Parent = plr
local stage = Instance.new("StringValue")
stage.Name = "Stage"
stage.Value = obbyData or 1
stage.Parent = ls
local char = plr.Character or plr.CharacterAdded:Wait()
char:waitForChild("HumanoidRootPart"):GetPropertyChangedSignal("CFrame")
char:WaitForChild("HumanoidRootPart").CFrame = checkpoints[stage.Value].CFrame
char.Humanoid.Touched:Connect(function(touch)
if touch.Parent == checkpoints then
if (tonumber(touch.Name) and tonumber(touch.Name) > tonumber(stage.Value)) or touch.Name == "End" then
stage.Value = touch.Name
pcall(function()
obbyDS:SetAsync(plr.UserId .. "-obbyStageProgress", plr.leaderstats.Stage.Value)
end)
end
end
end)
plr.CharacterAdded:Connect(function(char)
local hrp = char:WaitForChild("HumanoidRootPart")
local humanoid = char:WaitForChild("Humanoid")
hrp:GetPropertyChangedSignal("CFrame"):Wait()
hrp.CFrame = checkpoints[stage.Value].CFrame
humanoid.Touched:Connect(function(touch)
if touch.Parent == checkpoints then
if (tonumber(touch.Name) and tonumber(touch.Name) > tonumber(stage.Value)) or touch.Name == "End" then
stage.Value = touch.Name
pcall(function()
obbyDS:SetAsync(plr.UserId .. "-obbyStageProgress", plr.leaderstats.Stage.Value)
end)
end
end
end)
end)
Somebody explained it before: when closing the game, the game will prioritize on closing the game and will most likely disable some scripts (correct me if I’m wrong). So to save data anyway, you need to use game:BindToClose(). It would look like this:
game:BindToClose(function()
for i,player in pairs(game.Players:GetPlayers()) do
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Stage.Value
local success, errormessage = pcall(function()
saveStage:SetAsync(playerUserId, data)
end)
if success then
print("Saved stage")
else
warn(errormessage)
end
end
end