Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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.

0
Thank you, it works but the car rotated, where would I change the rotation Imgood543 21 — 3y
0
I edited my post to consider this as well. Vinceberget 1420 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
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

0
Workspace.Car.Script:6: invalid argument #2 (Vector3 expected, got CFrame), on line 6 Imgood543 21 — 3y

Answer this question