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

Loop inside of loop kills my game and I don't know why?

Asked by 6 years ago
Edited 6 years ago

I'm experimenting with loops inside of loops and this is my first attempt, but when I click, Studio will always crash and I don't know why. Here is script:

loop = 1

while true do
    while loop < 8 do
    wait(1)
    script.Parent.Parent.HingeConstraint.TargetAngle = script.Parent.Parent.HingeConstraint.TargetAngle - 6
    loop = math.random(1,10)
end

local CD = script.Parent -- Clickdetector

CD.MouseClick:connect(function(click)
        loop = math.random(1,7)
end)
end

1
Try adding a wait() right above the last end Impacthills 223 — 6y
0
why are you making your event inside loop?,you don't need 2 loop for this script TheSkyofIndia 150 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

An event will be activated when it's... activated.

You don't need a loop for that, also you forgot a wait() wich can also break your script.

loop = 1
local CD = script.Parent -- Clickdetector

while true do
    wait()
    while loop < 8 do
     wait(1)
         script.Parent.Parent.HingeConstraint.TargetAngle =              script.Parent.Parent.HingeConstraint.TargetAngle - 6
         loop = math.random(1,10)
    end
end

CD.MouseClick:connect(function(click)
        loop = math.random(1,7)
end)

Also, why do you have two loops inside each other??

Ad

Answer this question