Need help with invalid argument Vector3 expected, got nil

hi im tryng make an parto that move wethever theparts are
i am following this tutorial along Ski Lifts, Trains, Airplanes, Boats, Cars and more - Roblox Scripting Tutorial - YouTube

  1. when i run the game it only goes to 4 parts and the error say 19:32:36.395 ServerScriptService.Script:24: invalid argument #3 (Vector3 expected, got nil) - Server - Script:24



for i, folder in ipairs(workspace.Paths:GetChildren()) do
    
    local positions = {}
    
    for i, part in ipairs(folder:GetChildren()) do
        positions[tonumber(part.Name)] = part.Position
        --part:Destroy()
        part.CanCollide = false
    end
    
    local target = Instance.new("Part")
    target.Anchored = true
    target.CanCollide = false
    target.CanTouch = false
    target.Position = positions[1]
    target.Size = Vector3.new(4, 4, 4)
    target.Parent = folder
    
    spawn(function()
        local i = 1
        while true do
            i += 1
            if i > #positions then i = 1 end
            target.Position = positions[i]
            wait(2)
        end
    end)
    
    
    
    
    
end
for i, folder in ipairs(workspace.Paths:GetChildren()) do
	
	local positions = {}

	for i, part in ipairs(folder:GetChildren()) do
		if part:IsA("BasePart") then
			table.insert(positions, part.Position)
			--part:Destroy()
			part.CanCollide = false
		end
	end

	local target = Instance.new("Part")
	target.Anchored = true
	target.CanCollide = false
	target.CanTouch = false
	target.Position = positions[1]
	target.Size = Vector3.new(4, 4, 4)
	target.Parent = workspace

	task.spawn(function()
		local i = 1
		while task.wait(2) do
			i += 1
			if i > #positions then i = 1 end
			target.Position = positions[i]
		end
	end)
end
1 Like

Yes! Also remember to replace spawn with task.defer.

Perhaps not defer but thank you for reminding me to change spawn() to task.spawn().

Question, why is task.spawn needed here?

(in reference to the second paragraph)

To create a new thread to handle the execution of the code within the thread’s function body, meanwhile the main thread can continue with its own execution of the entire script instead of yielding when “task.wait(2)” is called.

1 Like