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

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

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:

RE.SpawnCar.OnServerEvent:Connect(function(player,carName)
	local spawnedCar = game:GetService("ReplicatedStorage"):WaitForChild("Vehicles"):FindFirstChild(carName):Clone()
	spawnedCar.Name = player.Name.."'s Car"
	spawnedCar.Parent = game:GetService("Workspace")
	spawnedCar:MakeJoints()
	local char = player.Character or player.CharacterAdded:Wait()
	spawnedCar:SetPrimaryPartCFrame(char.HumanoidRootPart.CFrame.LookVector + Vector3.new(0,3,10))
end)

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

✓ Accepted answer

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 CFrame.new().

spawnedCar:SetPrimaryPartCFrame(CFrame.new(char.HumanoidRootPart.CFrame.LookVector + Vector3.new(0,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.

4 comments

0

This didn't really work out. The car just goes to the Workspace and doesn't teleport infront of the character.

0

It also gives me no errors with this script.

0

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.

0

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.

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