Me and one of my friends are currently making an obby game. If you want to test it, the link to the game is here: Obby levels 1-10 TEST - Roblox. Anyway, I spent 3 days trying to figure out checkpoints and watching youtube videos of how to code it, and this was the one that worked: https://www.youtube.com/watch?v=c9lfIy5EyL8.
I was wondering how to make it save. Like, how do I make it so that if you leave the game, it keeps your checkpoint.
Thanks,
voidcrusher385
The code for the checkpoints is here:
local checkpointsFolder = game.Workspace.CheckpointsFolder
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Value = 1
stage.Parent = leaderstats
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
wait()
character:MoveTo(checkpointsFolder[stage.Value].Position)
humanoid.Touched:Connect(function(hit)
if hit.Parent == checkpointsFolder then
if tonumber(hit.Name) == stage.Value + 1 then
stage.Value += 1
end
end
end)
end)
end)```
Hey there! To make the player’s checkpoints save, you should use the DatastoreService. This code should do just that! Ensure you have access to API services enabled (go to game settings, security). Let me know whether this works. If it throws any error let me know!
local DDS = game:GetService("DataStoreService")
local players_checkpoints = DDS:GetDataStore("Checkpoints")
local checkpointsFolder = game.Workspace.CheckpointsFolder
game.Players.PlayerAdded:Connect(function(player)
local data = players_checkpoints:GetAsync(player.UserId) or {1}
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Value = data[1]
stage.Parent = leaderstats
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
wait()
character:MoveTo(checkpointsFolder[stage.Value].Position)
humanoid.Touched:Connect(function(hit)
if hit.Parent == checkpointsFolder then
if tonumber(hit.Name) == stage.Value + 1 then
stage.Value += 1
end
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {player.leaderstats.Stage.Value}
players_checkpoints:SetAsync(player.UserId, data)
end)
game:BindToClose:Connect(function()
for i, player in pairs(game.Players:GetChildren()) do
local data = {player.leaderstats.Stage.Value}
players_checkpoints:SetAsync(player.UserId, data)
end
end)
Thank you so much! Just checking, I should put this in serverscriptservice, right? And I can name the script whatever? Sorry if this is a dumb question, I am new to lua
Like, it just restarts me at the beginning like nothing happened
Also, all I did was put this script into serverscriptservice (I titled the script “Save”) and change the thing in settings. Was there something else I was supposed to do?