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.
-- 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
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
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
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)