Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i make a instance that when touched will make another instance and do the same?

I'm trying to make a part that when is touched it will make another part 4 studs in front of it (x), and if that part is touched will do the same thing, basically i'm trying to make a endless path that goes on forever if you try to go walk in that path.

But.. i just can't think of anything! my script will make a instance if the part is touched but that's it, i wanted to make that instance if touched will also make another instance with the same function.

local function touched ()
	local part = script.Parent:Clone()
	local position = script.Parent.Position
	part.Position = position + Vector3.new(0,0.5,4)
	-- what do i put here or outside of this function to make it that if the instance is touched it will  do the same thing again and again?
end

script.Parent.Touched:Connect(touched)

1 answer

✓ Accepted answer

You can do this by using a recursive function that will just call itself when a new part has been touched.


function waitForTouch (part)
	local connection;

	connection = part.Touched:Connect(function(hit)
		if hit and hit.Parent:FindFirstChildOfClass("Humanoid") then
			connection:disconnect();
			local newPart = part:Clone();
			newPart.CFrame = CFrame.new(newPart.CFrame.p + Vector3.new(0,0,4))
			newPart.Parent = part.Parent;
			part:Destroy()
			waitForTouch(newPart)
		end
	end)
end 
	
	
	
waitForTouch(workspace.Part)
	

This will create a new part and destroy the old one.

1 comment

0

:D thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.