Coroutine returns empty table despite having correct value

local SeekTargets = coroutine.create(function()
	while true do
		local Part = Instance.new("WedgePart")
		Part.CanCollide = false
		Part.Transparency = 0.5
		Part.Size = Vector3.new(3, 30, 30)
		Part.Position = script.Parent.Position + script.Parent.CFrame.LookVector * 21
		Part.Orientation = script.Parent.Orientation + Vector3.new(0, -45, 90)
		Part.Anchored = true
		Part.Parent = workspace
		local Parts = {}
		Part.Touched:Connect(function()
			local Parts = Part:GetTouchingParts()
			print(Parts)
		end)
		game.Debris:addItem(Part,0.5)
		coroutine.yield(Parts)
	end
end)

while true do
	local success, parts = coroutine.resume(SeekTargets)
	print(success , parts)
	if #parts>0 and success == true then
		local pos = parts[math.random(1,#parts)].Position
		local dir = pos-script.Parent.Position
		local ray = workspace:Raycast(script.Parent.Position ,parts[math.random(1,#parts)].Position)
		local point = dir
		print(ray)
		if ray then
			print(ray.Direction)
			point = ray.Position-script.Parent.Position
		end
		script.Parent.Parent.Humanoid:Move(-point)
	end
	wait(0.5)
end

Wheni print out parts i do get a list of touching parts, but when i print out Parts the table is empty.

you aren’t actually changing the Parts table
you are making a new local variable

2 Likes

Yeah but i get the parts touching it before destroying the part

Sharp! , That local lol. That should be it

also you are creating a new connection every loop in the while loop, which is obviously bad
Because a new wedge is created every loop

yea sorry about that I edited my reply