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

How would I make this rotation loop for a placing system more efficient?

Asked by 4 years ago
Edited 4 years ago

Hi, so I have been making a placing system for a game, and got stuck at rotation. After trying for a bit, I got this. But it is extremely inefficient and is very long. Any idea on how to make it more efficient?

Edit: Specifically the floor's rotation loop

Edit2: "floorRot" is a number that gets added to the model's CFrame via CFrame.Angles() whilst placing

mouse.KeyDown:Connect(function(k)
    if k:lower() == "r" and script.Parent.Parent.Parent.IsPlacing.Value then
        if type then
            if type == "floor" then
                isRotating = true
                while isRotating do
                    if floorRot == 0 then
                        floorRot = 10
                    elseif floorRot == 10 then
                        floorRot = 20
                    elseif floorRot == 20 then
                        floorRot = 30
                    elseif floorRot == 30 then
                        floorRot = 40
                    elseif floorRot == 40 then
                        floorRot = 50
                    elseif floorRot == 50 then
                        floorRot = 60
                    elseif floorRot == 60 then
                        floorRot = 70
                    elseif floorRot == 70 then
                        floorRot = 80
                    elseif floorRot == 80 then
                        floorRot = 90
                    elseif floorRot == 90 then
                        floorRot = 100
                    elseif floorRot == 100 then
                        floorRot = 110
                    elseif floorRot == 110 then
                        floorRot = 120
                    elseif floorRot == 120 then
                        floorRot = 130
                    elseif floorRot == 130 then
                        floorRot = 140
                    elseif floorRot == 140 then
                        floorRot = 150
                    elseif floorRot == 150 then
                        floorRot = 160
                    elseif floorRot == 160 then
                        floorRot = 170
                    elseif floorRot == 170 then
                        floorRot = 180
                    elseif floorRot == 180 then
                        floorRot = -170
                    elseif floorRot == -170 then
                        floorRot = -160
                    elseif floorRot == -160 then
                        floorRot = -150
                    elseif floorRot == -150 then
                        floorRot = -140
                    elseif floorRot == -140 then
                        floorRot = -130
                    elseif floorRot == -130 then
                        floorRot = -120
                    elseif floorRot == -120 then
                        floorRot = -110
                    elseif floorRot == -110 then
                        floorRot = -100
                    elseif floorRot == -100 then
                        floorRot = -90
                    elseif floorRot == -90 then
                        floorRot = -80
                    elseif floorRot == -80 then
                        floorRot = -70
                    elseif floorRot == -70 then
                        floorRot = -60
                    elseif floorRot == -60 then
                        floorRot = -50
                    elseif floorRot == -50 then
                        floorRot = -40
                    elseif floorRot == -40 then
                        floorRot = -30
                    elseif floorRot == -30 then
                        floorRot = -20
                    elseif floorRot == -20 then
                        floorRot = -10
                    elseif floorRot == -10 then
                        floorRot = 0
                    end
                    wait(0.2)
                end
            elseif type == "wall" then
                if wallRot == 0 then
                    wallRot = 90
                elseif wallRot == 90 then
                    wallRot = 180
                elseif wallRot == 180 then
                    wallRot = -90
                elseif wallRot == -90 then
                    wallRot = 0
                end
            end
        end

    end
end)

1 answer

Log in to vote
1
Answered by
sleazel 1287 Moderation Voter
4 years ago

To make your code shorter replace your if elseif statement with this:

floorRot = floorRot +10
if floorRot >180 then
    floorRot = -170
end
-------
wallRot = wallRot + 90
if wallRot > 180 then
    wallRot = -90
end

Now to make your code more efficient is another story. Unfortunately i do not know what floorRot is supposed to do, so I can't help you with that. However, for smooth and efficient rotation I suggest using TweenService, rather than making timed increments.

0
Oh, sorry! I edited my answer to show what it does. mybituploads 304 — 4y
0
Actually I don't think it can be more efficient anyways mybituploads 304 — 4y
Ad

Answer this question