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

Rotating a model?

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

✓ Accepted answer

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.

3 comments

0

Thanks! xD

0

You're welcome :D

0

How come everyone answers easy questions when I'm asleep!

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