How can I make a working obby spawn system?

I am trying to create an obby system where if you step on an object, in my case its a SpawnLocation, it would spawn you to that object when you die. Whenever I join the game, it sets my stage to a random checkpoint like 2. Here is my code:

local Players = game:GetService(“Players”)
local CheckpointsFolder = game.Workspace.Checkpoints
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local Stage = Instance.new(“IntValue”)
Stage.Parent = leaderstats
Stage.Name = “Stage”
player.CharacterAdded:Connect(function(char)
local checkpoint = CheckpointsFolder:FindFirstChild(Stage.Value)
player.Character.PrimaryPart.CFrame = checkpoint.CFrame
end)
end)
for _, v in pairs(CheckpointsFolder:GetChildren()) do
if v:IsA(“SpawnLocation”) then
v.Touched:Connect(function(part)
local player = Players:GetPlayerFromCharacter(part.Parent)
local stageValue = player.leaderstats.Stage
if player and stageValue.Value < tonumber(v.Name) then
stageValue.Value = v.Name
end
end)
end
end

I dont really know what I did wrong since I am kind of a beginner at this.

Please use lua formatting for code with normal tabs!

I don’t know why, when I moved this code to the test place, many errors appeared immediately. I don’t know why you don’t use PivotTo and forgot about the char argument, using player.Character instead, but it doesn’t matter. Anyway, here is a slightly tweaked script.

local Players = game:GetService("Players")
local CheckpointsFolder = game.Workspace:FindFirstChild("Checkpoints") or game.Workspace:WaitForChild("Checkpoints")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local stage = Instance.new("IntValue")
	stage.Parent = leaderstats
	stage.Name = "Stage"
	
	stage.Value = 1 -- DEFAULT VALUE! YOU CAN USE DATASTORES TO SAVE PLAYERS DATA!
	
	player.CharacterAdded:Connect(function(character : Model)
		local checkpoint = CheckpointsFolder:FindFirstChild(stage.Value)
		character:PivotTo(checkpoint.CFrame)
	end)
end)

for _, v in pairs(CheckpointsFolder:GetChildren()) do
	if v:IsA("SpawnLocation") then
		v.Touched:Connect(function(part)
			local player = Players:GetPlayerFromCharacter(part.Parent)
			
			if player then
				local leaderstats = player:FindFirstChild("leaderstats") or player:WaitForChild("leaderstats")
				local stageValue = leaderstats:FindFirstChild("Stage")

				if player and leaderstats and stageValue and stageValue.Value < tonumber(v.Name) then
					stageValue.Value = v.Name
				end
			end
		end)
	end
end

It works! Thank you and like I said, I am a beginner at this so I don’t really have THAT much knowledge.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.