I’m trying to make a platform that spawns a model, and I want it to be as reusable as possible, so I’m using the platform as a base. Right now, the platform clones the model in question (kept in ServerStorage) and moves it to the workspace, and it successfully moves the model to its current position.
Unfortunately, if the platform isn’t oriented the same as the model it spawns, it comes out sideways. I’ve been trying to use CFrame.lookAt to properly orient the model, but it can’t cast the CFrame to Vector3
Here’s the relevant code, that takes the model and puts it where it needs to be, and throws that Unable to cast CoordinateFrame to Vector3 error.
clone = Mod:clone()
location = Vector3.new(script.Parent.Position.X, script.Parent.Position.Y + 3, script.Parent.Position.Z)
point = Vector3.new(script.Parent.Parent["This Side Towards Enemy"].Position)
clone.Parent = workspace
clone:MoveTo(location)
clone:MoveTo(CFrame.lookAt(location, point))
clone:MakeJoints()
I tried using a CFrame to set both the position and the orientation at the same time, but it kept spawning the model anywhere but the intended position.
Right now I’m still trying to find ways to either give the model the same orientation as the spawn platform, or to have it pointing towards the red line I use to remember which way it’s pointing, any assistance from more experienced scripters is appreciated
To properly position and orient the model spawned by the platform, dude you can use a combination of CFrame and Vector3 functions. Here’s how you can modify your code to achieve the desired behavior:
local Mod = game.ServerStorage.ModelToSpawn -- Dude idk your Directory so Replace "ModelToSpawn" with the name of your model in ServerStorage
-- Clone the model
local clone = Mod:Clone()
-- Calculate the position and orientation of the model
local spawnPosition = script.Parent.Position + Vector3.new(0, 3, 0) -- Adjust the offset as needed
local lookAtPosition = script.Parent.Parent["This Side Towards Enemy"].Position
-- Move the clone to the calculated position
clone:SetPrimaryPartCFrame(CFrame.new(spawnPosition, lookAtPosition))
-- Parent the clone to the workspace
clone.Parent = workspace
-- Optionally, you can make joints if necessary
clone:MakeJoints()
Explanation:
We first clone the model (Mod:Clone()), which creates an exact duplicate of the model.
We calculate the spawnPosition by adding an offset of Vector3.new(0, 3, 0) to the platform’s position. Adjust the offset as needed to place the model correctly on top of the platform.
We determine the lookAtPosition by getting the position of the “This Side Towards Enemy” part. This is the position the model will face.
We use CFrame.new(spawnPosition, lookAtPosition) to create a CFrame that positions and orients the model correctly.
We set the model’s primary part’s CFrame to the calculated CFrame using clone:SetPrimaryPartCFrame(...). The primary part is usually the part that determines the position of the entire model.
Finally, we parent the clone to the workspace, and if needed, you can make joints (clone:MakeJoints()) if your model requires them.
In any event, that looks like a lot more fun to fix than the rotation, so that’s always a bonus
EDIT: The carhenge problem was because it was trying to face the exact position of the red line, I fixed it by offsetting that position by the same amount the cars are
EDIT 2: PivotTo is a 1:1 drop in replacement for SetPrimaryPartCFRame, while the latter does still work, I swapped it for the former just in case, which also works perfect. Marked RadAnimeBro’s reply as a solution