Infinite obby thing not working

im trying to make a system where when you touch pivot or the end part of a stage, it would spawn a new stage after it which is essentially infinite. but instead of the next part spawning new stages, only the first end part/pivot would trigger it

i tried using ai and it worked but the code seems to complicated for m level of understanding and I would not want to use AI in my games

--
local storage = workspace.ObbyStagesStorage
local children = storage:GetChildren()
local pivot
--

local function getRandomStage()
	local index = math.random(1, #children)
	return storage[index]
end
	
local function getNewStage()
	local chosen = getRandomStage()
	local newStage = chosen:Clone()
	
	newStage:PivotTo(pivot.CFrame)
	newStage.Parent = workspace
	
	pivot = newStage.EndPart
	
	return pivot
end

--

local debounce = true

local function onPivotTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == true then
		getNewStage()
		pivot = getNewStage()
		debounce = false
	else
		task.wait(2)
		debounce = true
	end
end

--

pivot = workspace.Spawn.EndPart

pivot.Touched:Connect(onPivotTouched)

this is how it looks like in game

1 Like

When you overwrite the variable “pivot”, that doesn’t mean that the.Touched:Connect defined on the last line will apply to the new pivot. You need to manually relisten to .Touched on the new pivot and drop the last Connection.

2 Likes