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

Model not rotating as a unit?

Asked by 6 years ago

I'm trying to make the entire model spin from a center point (which in this case I've labeled as spin.)

The tree of models is "SecurityCamera" -> "SecurityParts" AND "Spin"(Part) -> Script

The script is located under "Spin".

Not sure how to use "SetPrimaryPartCFrame", but I just know that's what you do for this situation.

Code -

local Main = script.Parent
while true do
    Main.SetPrimaryPartCFrame = script.Parent.SetPrimaryPartCFrame * CFrame.fromEulerAnglesXYZ(-.25,0,0)
    wait(0.01)
end

3 answers

Log in to vote
0
Answered by 6 years ago

Alright so I'm going to assume you've set the spin to be the primary part of security camera. In that case, you code should look like this:

--Server script under 'Spin'
local Main = script.Parent.Parent
while wait(0.1) do
    Main:SetPrimaryPartCFrame(script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(-0.25,0,0))
end

SetPrimaryPart is a built in roblox function, and not a property. Calling it sets the PrimaryPart of the model to the CFrame given, and everything follows. If you need to set the primary part of the model to the Spin part, then open properties and explorer. Find the model in explorer and click on it's PrimaryPart property. Once clicked, your cursor should have a logo next to it, then click the Spin part under explorer. The PrimaryPart property should now say 'Spin'. I hope this helped, please accept the answer if it did!

0
This worked for me, thanks for the help! noob1126 34 — 6y
0
No problem! iamnoamesa 674 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Make sure the model has its PrimaryPart declared (click on the Model in Explorer and look at the Properties window -- if PrimaryPart's value is empty, you need to define it (click on the empty value, your cursor will change, then click on the central part of your model).

The problem with your script is that you're trying to multiply a function "SetPrimaryPartCFrame" and a CFrame. Also, when calling functions, you need to use ":", not "." . Instead, do:

Main:SetPrimaryPartCFrame = Main:GetPrimaryPartCFrame() * CFrame.fromEulerAnglesXYZ(-.25,0,0)

Note the GetPrimaryPartCFrame() call after the '='. The logic of this line is "Set the cframe to whatever the current cframe is multiplied by this other CFrame"

0
Thanks for the guidance, this would've been correct as well. noob1126 34 — 6y
0
That would give you an error, since GetPrimaryPart does not return anything and is a void function. iamnoamesa 674 — 6y
0
Log in to vote
0
Answered by 6 years ago

Hi!

I believe you mixed up "SetPrimaryPartCFrame" with "PrimaryPart". SetPrimaryPartCFrame is a void function, while PrimaryPart is the property.

There is a different, more recommended method made by TigerCaptain (I credit him for this script!) This is a function that find all the baseparts in the model that follows the offset in unity with the PrimaryPart. This script will use ToObjectSpace() that converts world coordinates to object coordinates, to find the offset between any part and the PrimaryPart. The model MUST have a PrimaryPart otherwise this will not work.

I suggest you examine the script to learn along the way.

local Main = script.Parent

local function tweenModel(model)
    if not model.PrimaryPart then return end --If the model doesn't have a PrimaryPart then end.
    local details = {}
    local function getParts(par, t) --The function to get all the parts inside the model
        local t = t or {}
        for i,v in pairs(par:GetChildren()) do --For every child in the model do
            if v:IsA("BasePart") then --The object is a basepart
                t[#t+1]=v
            else
        t = getParts(v, t) --getParts is the variable for all baseparts within the model.
            end end
        return t
    end
    for i,part in pairs(getParts(model)) do
        if part ~= model.PrimaryPart then --The basepart is not the PrimaryPart
            details[part] = model.PrimaryPart.CFrame:toObjectSpace(part.CFrame) --Transforms CFrame from world to object coordinates. This is the offset.
        end end
    local con = model.PrimaryPart.Changed:connect(function()
        for part,offset in pairs(details) do
            part.CFrame = model.PrimaryPart.CFrame*offset --All the surrounding parts will the follow the PrimaryPart's offset.
    end end)
    while wait() do --This is where you can change the PrimaryPart and the other parts will follow.
    Main.PrimaryPart.CFrame = Main.PrimaryPart.CFrame * CFrame.fromEulerAnglesXYZ(-.25,0,0) --Play the rotation method.
    end
end

tweenModel(Main) --Play the function, Main is the model.

Click "Accept Answer" if this was the answer you were looking for! Comment if you have any more concerns!

0
I appreciate the help but I couldn't get this to work. noob1126 34 — 6y

Answer this question