Hi, I am making an obby and I need to make a button that will save your progress on what stage you are in. I’m not trying to make an auto save because I want people to replay the game from the beginning if they are interested to. Should I make a gui and then enter the correct script into it? Or is there something else I should do?
This is the auto saving code that I have, but would like to change it a little (if possible) so it could complete my wantings.
local DataStoreService = game:GetService("DataStoreService")
local StageDataStore = DataStoreService:GetDataStore("StageData")
local function TouchedPart(part, touchedPart)
local ply = game.Players:GetPlayerFromCharacter(touchedPart.Parent)
if ply then
if ply.leaderstats.Stage.Value < tonumber(part.Name) then
ply.leaderstats.Stage.Value = tonumber(part.Name)
end
end
end
local function AddTouchStageGiver()
local stageNum = 1
while true do
local part = game.Workspace:FindFirstChild(tostring(stageNum))
if part then
part.Touched:connect(function (touchedPart)
TouchedPart(part, touchedPart)
end)
else
break
end
stageNum = stageNum + 1
wait()
end
end
local function GetSavedStage(ply)
return StageDataStore:GetAsync(tostring(ply.UserId)) or 1
end
local function SaveCurrentStage(ply)
StageDataStore:SetAsync(tostring(ply.UserId), ply.leaderstats.Stage.Value)
end
local SPAWN_OFFSET = Vector3.new(0, 5, 0)
local function GetStageSpawnCFrames(stage)
local stageSpawnPart = game.Workspace:FindFirstChild(tostring(stage))
if not stageSpawnPart then
return game.Workspace:FindFirstChild("1").CFrame + SPAWN_OFFSET
end
return stageSpawnPart.CFrame + SPAWN_OFFSET
end
local stageValues = {}
local function CharacterAdded(ply, char)
repeat wait() until stageValues[ply]
char:SetPrimaryPartCFrame(GetStageSpawnCFrames(stageValues[ply].Value))
end
game.Players.PlayerAdded:connect(function (ply)
local leaderstats = Instance.new("ObjectValue", ply)
leaderstats.Name = "leaderstats"
ply.CharacterAdded:connect(function (char)
CharacterAdded(ply, char)
end)
local stage = Instance.new("IntValue", leaderstats)
stage.Name = "Stage"
stage.Value = GetSavedStage(ply)
stageValues[ply] = stage
--[[
if ply.Character then
CharacterAdded(ply, ply.Character)
end
--]]
end)
game.Players.PlayerRemoving:connect(function (ply)
SaveCurrentStage(ply)
end)
wait(1)
AddTouchStageGiver()
I appreciate your help, thank you!