Help with infinite yield problem

I am trying to create a script where a part travekls smoothly to another using pathfinding service and when the distance between 2 nodes is more than a set amount then it tweens istance of teleports but the problem I am having is that when a tween needs to happen when the tween is completed it just stops and does not continue teleporting

I am getting to errors with my script and it just stops

here is my code

local PathfindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local Player = workspace.Player
local Part = workspace.Part

local Path = PathfindingService:CreatePath({
	AgentRadius = 2,
	AgentHeight = 3,
	AgentCanJump = true
})

local Debounce = false
local Tween = nil
local OldPosition = nil
local MaximumTeleportDistance = 5

while true do
	if Player.Position ~= OldPosition then
		if Debounce then
			Debounce = false
		end
		Path:ComputeAsync(Part.Position, Player.Position)
		OldPosition = Player.Position
	end
	if not Part.Anchored then
		Part.Anchored = true
	end
	if not Debounce then
		for Index, Value in ipairs(Path:GetWaypoints()) do
			if Path:GetWaypoints()[Index + 1] then
				local Magnitude = (Value.Position - Path:GetWaypoints()[Index + 1].Position).Magnitude
				if math.floor(Magnitude) >= MaximumTeleportDistance then
					Tween = TweenService:Create(Part, TweenInfo.new(5), {
						Position = Path:GetWaypoints()[Index + 1].Position + Vector3.new(0, 4.5, 0)
					})
					Tween:Play()
				else
					if Tween then
						Tween.Completed:Wait()
					end
					warn("Tping!")
					local Part = Instance.new("Part")
					Part.Size = Vector3.new(1, 1, 1)
					Part.CFrame = CFrame.new(Value.Position) + Vector3.new(0, 4.5, 0)
					Part.CanCollide = false
					Part.Anchored = true
					Part.Parent = workspace
					Part.CFrame = CFrame.new(Value.Position) + Vector3.new(0, 4.5, 0)
				end
			end
			wait()
		end
	end
	if not Debounce then
		Debounce = true
	end
	RunService.Heartbeat:Wait()
end

I just don’t understand it Player and Part are both baseparts.

Please Help me I would apppreciate it a lot becausew I have been trying to fix this issues for days now and i cannot seem to wrap my head around it.

I have ntoiced that it does not finished the Path:GetWaypoints loop so aybe there is a infinite yield

1 Like

Can you show the error and an image of player and part in explorer?

1 Like

There is no error, These are the players

EVertime the gap between nodes is bigger than 5 studs it tweens instead of teleports to the node but when the tween finishes it does not continute to telepeort to the position it just stops and yeilds infinitely I think this is because of Tween.Completed:Wait()

1 Like