I am creating an obby game and what should we do if a person leaves the game half completed and when he comes back he need to start the game from the beginning. How exactly do we remove that and make it so that when he joins ,he joins from the same exact checkpoint that he left from?
(btw this is my game obby - Roblox so far its not completed you can test it if you want)
You can use DataStoreService to store player data, and where he was left.
Server Script:
--ServerScriptService
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local StageData = DataStoreService:GetDataStore("PlayerStage")
local StageSpawns = workspace.Obby.StageSpawns -- Your path to the spawns here
Players.PlayerAdded:Connect(function(player: Player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local stage = Instance.new("NumberValue", leaderstats)
stage.Value = 0
stage.Name = "Stage"
-- Get the stage value and pivot the player there
repeat wait() until player.Character
local success, result = pcall(function()
return StageData:GetAsync(player.UserId)
end)
if success and result ~= nil then
stage.Value = result
local stageSpawn = stageSpawns:FindFirstChild(tostring(stage.Value)
if stageSpawn == nil then return end
player.Character:WaitForChild("HumanoidRootPart").Position = stageSpawn.Position
end
end)
Players.PlayerRemoving:Connect(function(player: Player)
pcall(function()
return StageData:SetAsync(player.UserId, stage)
end)
end)