Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Whenever I run this script that makes multiple parts move, why doesn't it work?

Whenever this runs, I get this.

`18:18:45.775 - Workspace.Fly:11: bad argument #2 to '?' (Vector3 expected, got CFrame)
18:18:45.776 - Stack Begin
18:18:45.777 - Script 'Workspace.Fly', Line 11 - global startLoop
18:18:45.777 - Script 'Workspace.Fly', Line 24
18:18:45.778 - Stack End`

Why is it doing this?

Here is the script.

-- I could've used pairs but I was in a rush
local currentpos = Vector3.new(-5, 0.7, 0)
local parts = game.Workspace.ElectricityObsorber:GetChildren()
game.Workspace.ElectricityObsorber:MoveTo(currentpos)

function startLoop()
	wait(10)
	for i = 300,0,-1 do
		wait(0.01)
		for i = 1,#parts do
			local cf = parts[i].CFrame
			parts[i].CFrame = cf + CFrame.new(0,0,0.5)
		end
	end
	for i = 300,0,-1 do
		wait(0.01)
		for i = 1,#parts do
			parts[i].CFrame = parts[i].CFrame + CFrame.new(0.03,0,1,0.1)
		end
	end
end

while true do
	wait(0.01)
	startLoop()
end

2 comments

0

Add using a Vector, not a CFrame. ie; parts[i].CFrame = cf + Vector3.new(0,0,0.5)

0

You can't add CFrames.

1 answer

✓ Accepted answer

What the problem is that you can't add CFrames to CFrames. Instead, you have to add a CFrame to a Vector3, like so:

local cfr = CFrame.new(1,1,1)
local newCfr = cfr + Vector3.new(5,5,5)
print(newCfr) --prints 6,6,6

Thus your code should look like this:

-- I could've used pairs but I was in a rush
local currentpos = Vector3.new(-5, 0.7, 0)
local parts = game.Workspace.ElectricityObsorber:GetChildren()
game.Workspace.ElectricityObsorber:MoveTo(currentpos)

function startLoop()
    wait(10)
    for i = 300,0,-1 do
        wait(0.01)
        for i = 1,#parts do
            local cf = parts[i].CFrame
            parts[i].CFrame = cf + Vector3.new(0,0,0.5) --notice the difference here
        end
    end
    for i = 300,0,-1 do
        wait(0.01)
        for i = 1,#parts do
            parts[i].CFrame = parts[i].CFrame + Vector3.new(0.03,0,1,0.1) --and here
        end
    end
end

while true do
    wait(0.01)
    startLoop()
end

This discussion is preserved as it was. New answers, comments, and votes are closed.