New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

Why Won't My .LookVector Cast To A HumanoidRootPart.CFrame?

Asked by 9 days ago
Edited 9 days ago

I'm trying to create a Car Spawner, and it works fine. But I want the car to spawn infront of them.

This is my script that's causing an error:

    local spawnedCar = game:GetService("ReplicatedStorage"):WaitForChild("Vehicles"):FindFirstChild(carName):Clone()
    spawnedCar.Name = player.Name.."'s Car"
    spawnedCar.Parent = game:GetService("Workspace")
    local char = player.Character or player.CharacterAdded:Wait()
    spawnedCar:SetPrimaryPartCFrame(char.HumanoidRootPart.CFrame.LookVector +,3,10))

And this is the error I am getting:

09:18:00.788 - Unable to cast Vector3 to CoordinateFrame

09:18:00.789 - Stack Begin

09:18:00.789 - Script 'ServerScriptService.EventHandler', Line 7

09:18:00.789 - Stack End

Any help?

1 answer

Log in to vote
Answered by
y3_th 130
9 days ago

What you're trying to do is pass a Vector3 value into a function that takes a CFrame value. In order to solve your problem, all you need to do is to create a CFrame from the Vector3, which can be done by wrapping the Vector3 in

spawnedCar:SetPrimaryPartCFrame( +,3,10))) 

There's a really good answer on here about the difference between CFrame and Vector3, and I'd suggest for you to read it.

This didn't really work out. The car just goes to the Workspace and doesn't teleport infront of the character. killerbrenden 336 — 9d
It also gives me no errors with this script. killerbrenden 336 — 9d
Your issue had to do with casting a Vector3 to a CFrame, not an issue with your math. If you wanted the car to spawn in front of the HumanoidRootPart, what you need to do is to add the HumanoidRootPart's position to the HumanoidRootPart's LookVector. y3_th 130 — 9d
Errors aren't the end-all-be-all of scripting since they can't point out unintentional behavior. They can only point out errors with compiling and running your script, such as issues with casting and syntax. y3_th 130 — 9d

Answer this question