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

Moving a car then teleporting it back its original position not working?

I get this error Unable to cast Vector3 to CoordinateFrame

local model = game.Workspace.Car
local originallocation = model.PrimaryPart.CFrame

while true do
    for i = 0,100,0.1 do
        model:SetPrimaryPartCFrame(model.PrimaryPart.Position + Vector3.new(1,0,0))  
        wait()
    end
    model:SetPrimaryPartCFrame(originallocation)
end

2 answers

✓ Accepted answer

The problem is that you're trying to set the CFrame of the model to a Vector3. This doesn't work since CFrames have additional properties besides a Vector3 position.

However, you can create a CFrame from a Vector3 using CFrame.new. NOTE: by default, this will set the CFrame's orientation to 0,0,0.

local model = game.Workspace.Car
local originallocation = model.PrimaryPart.CFrame

while true do
    for i = 0,100,0.1 do
        model:SetPrimaryPartCFrame(CFrame.new(model.PrimaryPart.Position + Vector3.new(1,0,0)))  
        wait()
    end
    model:SetPrimaryPartCFrame(originallocation)
end

Changing the rotation is quite simple. The CFrame.Angles function will modify the orientation of any other CFrame, by simply multiplying both of them together.

CFrame.new(0,0,0) * CFrame.Angles(0,90,0)

But keep in mind that this function expects the inputs to be in radians, not in degrees. So the rotation might seem a bit, off. But fortunately there's a function that converts degrees into radians, as well as a function which does the opposite. It's math.rad and math.deg.

math.rad expects an input in degrees and converts it into an output in radians, which is exactly what we want.

CFrame.new(0,0,0) * CFrame.Angles(math.rad(0),math.rad(90),math.rad(0)) -- Rotation should be "Fixed" now!

Here's your script but with the changes:

local model = game.Workspace.Car
local originallocation = model.PrimaryPart.CFrame

while true do
    for i = 0,100,0.1 do
        model:SetPrimaryPartCFrame(CFrame.new(model.PrimaryPart.Position + Vector3.new(1,0,0)) * CFrame.Angles(math.rad(0),math.rad(90),math.rad(0)) )  
        wait()
    end
    model:SetPrimaryPartCFrame(originallocation)
end

You'll have to play around with the values to find the correct orientation.

2 comments

0

Thank you, it works but the car rotated, where would I change the rotation

0

I edited my post to consider this as well.

local model = game.Workspace.Car
local originallocation = model.PrimaryPart.CFrame
	 
while true do
	for i = 0,100,0.1 do
		model:SetPrimaryPartCFrame(model.PrimaryPart.Position + CFrame.new(1,0,0)) 
		wait()
	end
	model:SetPrimaryPartCFrame(originallocation)
	wait()
end


Try this not 100% though if dosn't work could you say which line the error is on

1 comment

0

Workspace.Car.Script:6: invalid argument #2 (Vector3 expected, got CFrame), on line 6

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