How to solve this? Unknown global 'target'

So, im making just a script that moves along parts under a path. I dont think it matters too much. Straight to the point, I made a variable for Instance.new("Part") called “Target”

One of the lines in the code says it can’t find it. Ive marked where its having trouble.

for i = 1, 3 do
		local target = Instance.new("Part")
		target.Anchored = true
		target.CanCollide = false
		target.CanTouch = false
		target.CFrame = startCFrame
		target.Size = Vector3.new(0, 0, 0)
		target.Transparency = 1
		target.Parent = folder

		local attachment1 = Instance.new("Attachment", target)

		local model = game.ServerStorage.Models[folder.Name]:Clone()
		model:SetPrimaryPartCFrame(target.CFrame)
		model.Parent = folder

		local attachment0 = Instance.new("Attachment", model.PrimaryPart)

		local alignPosition = Instance.new("AlignPosition")
		alignPosition.Attachment0 = attachment0
		alignPosition.Attachment1 = attachment1
		alignPosition.MaxForce = math.huge
		alignPosition.MaxVelocity = speed
		alignPosition.Responsiveness = 200
		alignPosition.Parent = folder

		local alignOrientation = Instance.new("AlignOrientation")
		alignOrientation.Attachment0 = attachment0
		alignOrientation.Attachment1 = attachment1
		alignOrientation.MaxTorque = math.huge
		alignOrientation.MaxAngularVelocity = 5
		alignOrientation.Responsiveness = 10
		alignOrientation.Parent = folder
		
	end

	table.insert(targets, target) ---------------RIGHT HERE

	spawn(function()
		wait(5)
		local i = 1
		while true do
			local nextI = i + 1
			if nextI > #positions then nextI = 1 end

			local magnitude = (positions[nextI] - positions[i]).Magnitude
		

			i = nextI
			
			spawn(function()
				for i, target in ipairs(targets) do
					target.CFrame = cFrames[nextI]
			
					wait(waitTime)
				end
			end)
			
			

			
			wait(magnitude / speed)
		end
	end)
end

Unknown global “Target”

Thanks

I would recommend hitting shift+alt+F in the script editor, as it will auto format everything and make issues like this more noticable.

1 Like

The issue is the scope, your variable is in a child scope where table.insert isn’t. Move table.insert inside the same scope

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.