How to change it, so new model gets added after gets close enough

local obbyNames = {"Obby 1", "Obby 2", "Obby 3", "Obby 4", "Obby 5", "Obby 6"}
local lastObbyPosition = Vector3.new(0, 0, 0)
local offset = Vector3.new(0, 0, 15)

while true do
	local random = math.random(1, #obbyNames)
	local obby = game.ReplicatedStorage[obbyNames[random]]:Clone()
	obby.Parent = workspace
	if obby then
		obby:SetPrimaryPartCFrame(CFrame.new(lastObbyPosition) * CFrame.new(offset))
		lastObbyPosition = obby:GetPrimaryPartCFrame().Position 
	end
	wait(10) 
end

I want to change this code, so new model (obby) gets added after player gets close enough, for example 10 studs.

How about adding a part 10 studs away and use a touch event?

Thats a good idea, I didnt think of that, but how to do it? Every obby has other parts and I only want, so the newest one has touch detector

I would really appreciate fast response from anyone

You can use DistanceFromCharacter

In which line of code should I put it?

Just add an if statement like so:

-- Change TARGETPOS to the position from where this check should originate from
if game.Players.LocalPlayer:DistanceFromCharacter(TARGETPOS) <= 10 then
	-- Spawn an obby
end

Also change game.Players.LocalPlayer is a variable for localscript, otherwise you have to specify from which player are you measuring the distance

Yeah I know LocalPlayer is variable for localscript in which is problem. It has to be in server script

Then, as mentioned before, just spawn a part using Instance.new and add a Touched event, then assign it to a variable outside of the loop, like you do with lastObbyPosition, and in the touched event, destroy the old part

local touchPart = nil

createTouchPart = function(position, size)
	if touchPart then
		touchPart:Destroy()
	end
	
	touchPart = Instance.new("Part", workspace)
	touchPart.Anchored = true
	touchPart.Position = position
	touchPart.Size = size
	touchPart.CanCollide = false
	touchPart.Transparency = 1
	
	touchPart.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			-- Call this function again to reset the touchPart
			createTouchPart(position, size)

			-- Spawn obby here
		end
	end)
end

-- Call the function first, to start the process
createTouchPart(YourPositionHere, YourSizeHere) -- Change the values here

About those values at the end, change those values to what?

To where the touchpart should spawn at and what the size of it should be, you should also change them inside the function, in the touched event

local obbyNames = {"Obby 1", "Obby 2", "Obby 3", "Obby 4", "Obby 5", "Obby 6"}
local lastObbyPosition = Vector3.new(0, 0, 0)
local offset = Vector3.new(0, 0, 15)
local touchPart = nil

createTouchPart = function(position, size)
	if touchPart then
		touchPart:Destroy()
	end

	touchPart = Instance.new("Part", workspace)
	touchPart.Anchored = true
	touchPart.Position = position
	touchPart.Size = size
	touchPart.CanCollide = false
	touchPart.Transparency = 1

	touchPart.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			-- Call this function again to reset the touchPart
			createTouchPart(position, size)

			while true do
				local random = math.random(1, #obbyNames)
				local obby = game.ReplicatedStorage[obbyNames[random]]:Clone()
				obby.Parent = workspace
				if obby then
					obby:SetPrimaryPartCFrame(CFrame.new(lastObbyPosition) * CFrame.new(offset))
					lastObbyPosition = obby:GetPrimaryPartCFrame().Position 
				end
				wait(0.5) 
			end
		end
	end)
end

createTouchPart(lastObbyPosition, Vector3.new(4, 1, 4.3))

I made it something like this (idk is it good), but after I touch that part, those part spawn infinitely

Because in the Touched event, you are calling the function with the exact same arguments (positon, size) You should change those parameters (on Line 22) to where you want the new touchpart to spawn

Those should be the same like in the last line?

No, it should be for example a position of your new obby, or just the position of where you want this part to be (Where should the player be for the next obby to spawn)

So for example, new position of obby would be that lastObbyPosition variable?

Ok, I figured it out, thank you for help

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