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

Rotating a model not going to precise location?

Asked by
NotSoNorm 777 Moderation Voter
8 years ago

Ok so I can rotate this model freely with arrow keys but I wrote up the returning to original fixture script and sometimes it goes to the right place other times it's tilted or in the wrong position/caught in an infinite loop.

How do I fix this?

local desiredrestrotation = Vector3.new(0, 65, 0) --Position it should fix to (note this is rotation not position)

local running = false
function returnoriginalfix()
    if up == false and down == false and right == false and left == false and running == false then
        running = true
        local x = false local y = false local z = false     

        --Fix Y axis
        spawn(function()
            repeat
                wait()
                if math.floor(controlbrick.Rotation.Y) > desiredrestrotation.Y then
                    db:SetPrimaryPartCFrame(controlbrick.CFrame * CFrame.Angles(0, math.rad(-.5), 0))
                else
                    db:SetPrimaryPartCFrame(controlbrick.CFrame * CFrame.Angles(0, math.rad(.5), 0))
                end
                y = true
            until math.floor(controlbrick.Rotation.Y) == desiredrestrotation.Y
        end)

        --Fix X axis
        spawn(function()
            repeat
                wait()
                if math.floor(controlbrick.Rotation.X) > desiredrestrotation.X then
                    db:SetPrimaryPartCFrame(controlbrick.CFrame * CFrame.Angles(math.rad(-.5), 0, 0))
                else
                    db:SetPrimaryPartCFrame(controlbrick.CFrame * CFrame.Angles(math.rad(.5), 0, 0))
                end
            until math.floor(controlbrick.Rotation.X) == desiredrestrotation.X
            x = true
        end)

        --Fix Z axis
        spawn(function()
            repeat
                wait()
                if math.floor(controlbrick.Rotation.Z) > desiredrestrotation.Z then
                    db:SetPrimaryPartCFrame(controlbrick.CFrame * CFrame.Angles(0, 0, math.rad(-.5)))
                else
                    db:SetPrimaryPartCFrame(controlbrick.CFrame * CFrame.Angles(0, 0, math.rad(.5)))
                end
            until math.floor(controlbrick.Rotation.Z) == desiredrestrotation.Z
            z = true
        end)

        --check for fixed orientation
        repeat wait() until x == true and y == true and z == true
        x = false y = false z = false 
        running = false
    end
end
0
I think because of lag, you might want to just check if you're close to the value, instead of trying to get an exact match of X,Y,Z at the same time. Once they're all close in value, just set the CFrame to a backup of the original CFrame. This probably explains the infinite loop issues, as you may be running into instances in which X,Y,Z are never an exact match at the same time to the original JasonTheOwner 391 — 8y

Answer this question