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

How to rotate an object when it is spawned in?

Asked by 6 years ago

In my ServerStorage, when you spawn in a model to a certain position, it is rotated a certain way (which is 90 degrees off). It is not clear to me how this would be done. It cannot be as simple as rotating the model from the ServerStorage. It has to be done by a script, or else it won’t work in other places that you spawn it. Here is a portion of it.

01bg.S.MouseButton1Click:connect(function()
02for i, v in pairs(game.Teams['Garage 6']:GetPlayers()) do
03if (locked) then return end
04locked = true
05local vehicleName = player.Name.."Car"
06if (not workspace:FindFirstChild(vehicleName)) then
07local model = game.ServerStorage.Vehicles:FindFirstChild(vehicles[index][1]):Clone()
08model.Name = vehicleName
09model.Parent = game.Workspace
10model:MakeJoints()
11model:MoveTo(Vector3.new(-519.846, 186.25, -88.916))

Any help is appreciated. Thank you.

0
I know it’s a short answer I’m on phone but use cframe. vissequ 105 — 6y

1 answer

Log in to vote
3
Answered by 6 years ago
Edited 2 years ago

Use :SetPrimaryPartCFrame() to position it before parenting it to the workspace.
https://developer.roblox.com/api-reference/function/Model/SetPrimaryPartCFrame

CFrame values can both set a position of a part as well as the orientation of it. If your Model has a PrimaryPart, this function will set the CFrame of the PrimaryPart to where ever you choose and then take all the parts in that model and move them based on where the PrimaryPart is.

01local Model = workspace.Car
02Model.PrimaryPart = Model.Center
03 
04local Clone = Model:Clone()
05Clone:MakeJoints()
06 
07local spawnPosition = CFrame.new(10, 5, 10) * CFrame.Angles(0, math.rad(90), 0)
08--CFrame.new is the position
09--CFrame.Angles is the orientation
10--math.rad converts degrees to radians
11 
12Clone:SetPrimaryPartCFrame(spawnPosition)
13Clone.Parent = workspace
Ad

Answer this question