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

Rotating a model?

Asked by 9 years ago

Let say I have a model with a few blocks within it, and I want to rotate that model through script. How would I go about that? (Just so ya'll know, I have checked any wiki and previous question for an answer and haven't got a clear one)

My attempt:

RotationDevice = game.Workspace.Model

function Rotate(objects,center,new) 
    for i,object in pairs(objects:GetChildren()) do 
        if object.ClassName == "Part" then 
            object.CFrame = new:toWorldSpace(center:toObjectSpace(object.CFrame)) 
        end 
    end 
end

for a,z in pairs (RotationDevice:GetChildren()) do
Rotate(RotationDevice, v.Position, CFrame.new(0,0,0))
end

Returns: toObjectSpace is not a valid member

Any help is appreciated

1 answer

Log in to vote
3
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

If you simply just want to rotate it, I suggest to define the model's PrimaryPart. What this does is it gives you the option to use the :SetPrimaryPartCFrame() method.

YourModel = workspace.Model

function ReturnPrimaryPart(Model)
    local ModelTable = Model:GetChildren()
    local PickAPart = ModelTable[math.random(1, #ModelTable)]
    return PickAPart
end

Model.PrimaryPart = ReturnPrimaryPart(YourModel)
-- Note: You could've just as easily set the PrimaryPart of a model by using the command line in studio.

function RotateModel(Model, RotateX, RotateY, RotateZ)
    Model:SetPrimaryPartCFrame(Model.CFrame * CFrame.Angles(math.rad(RotateX), math.rad(RotateY), mathrad(RotateZ)))
end

RotateModel(YourModel, 0, 30, 0)
-- This will rotate YourModel 30 degrees in the Y-axis.
0
Thanks! xD BSIncorporated 640 — 9y
0
You're welcome :D Redbullusa 1580 — 9y
0
How come everyone answers easy questions when I'm asleep! EzraNehemiah_TF2 3552 — 9y
Ad

Answer this question