--Let's define a function that runs whenever something touches the teleporter part


function onTouch(part)
	if (part == nil or part.Parent == nil) then return end --Failsafe in case the part is destroyed for some reason
	
	--More like "teleport only humanoids" but okay
	if (config.TeleportOnlyPlayers.Value) then
		if (part.Parent:findFirstChild("Humanoid") == nil) then return end --Stop if we can't find a humanoid
	end
	
	--Wait for the part to move towards the center of the part (or be removed)
	while true do
		wait(0.1)
		--These are the conditions the part needs to meet to continue
		if (((telePart.Position-part.Position)*Vector3.new(1,0,1)).magnitude < 1.5 and 
			part.Velocity.magnitude < 0.5 and
			not isBusy) then break end
		
		--These are the conditions under which the part is disqualified
		if (((telePart.Position-part.Position)*Vector3.new(1,0,1)).magnitude > 10) then return end --Stop because they walked away
		if (part.Parent == nil) then return end --Don't care about it if it's removed
	end
	
	if (part.Parent == nil) then return end --Stop if the part was removed
	
	--Find the other teleporter	
	local target = Register:GetTeleporter(config.Destination.Value)
	if (target == nil) then return end --Do nothing if it can't find it
	
	indicator.BrickColor = BrickColor.new("Really red") --Turn the indicator red to let everyone know we're busy
	
	isBusy = true --Set this to true so we don't teleport multiple things at once	
	wait(1) --Wait one second for dramatic effect
	
	--We need to check again *sigh* because they might have walked off
	if (((telePart.Position-part.Position)*Vector3.new(1,0,1)).magnitude >= 1.5 or part.Velocity.magnitude >= 0.5) then 
		indicator.BrickColor = BrickColor.new("Lime green") --Turn the indicator green to let everyone know we finished
		isBusy = false --Also disable the debounce
		return 
	end
	
	--Now get all the parts attached to the one we're teleporting
	local parts = {}
	getAssemblyParts(part,parts)
	
	--Also check if there's a humanoid so it doesn't do its no-physics walk magic
	local humanoid = part.Parent:findFirstChild("Humanoid")
	local walkspeed = 0 --Save the walkspeed of any humanoids (keep this for scope)
	if (humanoid) then
		walkspeed = humanoid.WalkSpeed --Here we go, let's save this
		humanoid.WalkSpeed = 0 --Then we make sure they can't run away
	end
	
	--Anchor the assembly and figure out where to put all the parts
	local cframes = {}
	for _,v in pairs(parts) do
		v.Anchored = true
		cframes[v] = telePart.CFrame:toObjectSpace(v.CFrame)
	end
	
	--Play a sound and do a super fancy effect
	Networking.StartEffect:FireAllClients(part,telePart,target.Parts.Teleporter)
	glowPart.Transparency = 1
	
	target.Scripting.ForceSetBusy:Fire() --Tell the other teleporter that we're coming!
	wait(4.5) --Wait for all of that to finish
	for _,v in pairs(parts) do
		v.CFrame = (target.Parts.Teleporter.CFrame * CFrame.new(0,1,0)) * cframes[v]
		v.Anchored = false
	end
	
	--Change the walkspeed back if we changed it earlier
	if (humanoid) then
		humanoid.WalkSpeed = walkspeed
	end
	
	wait(3)
	
	glowPart.Transparency = 0
	indicator.BrickColor = BrickColor.new("Lime green") --Turn the indicator green to let everyone know we finished
	isBusy = false
end

telePart.Touched:connect(onTouch) --Connect the touch event to the function we defined