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

Studio always crashes,I don't know how to do a different technique?

Asked by 6 years ago

When I click, studio either wants me to break/kill the script or it just crashes. I don't know how to do the same script with a different technique, but the loop inside of a loop is not working. Here's script:

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

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

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

0
You sure the wait(1) in line 13 is in the correct position? (I dont know if its the problem) HeyItzDanniee 252 — 6y

3 answers

Log in to vote
0
Answered by
Nonaz_jr 439 Moderation Voter
6 years ago
Edited 6 years ago

The reason your script crashes is because you end up in an infinite loop without any wait statements in it. Let's walk through your script.


loop = 1 local CD = script.Parent -- Clickdetector while true do while loop < 8 do

so far all is good, a double loop is no problem as long as each either breaks out of the loop or has a wait statement.

Note that a wait statement in the inner loop makes the outer loop wait as well as long as the inner loop runs.

Then the second part goes:

    wait(1)
    script.Parent.Parent.HingeConstraint.TargetAngle = script.Parent.Parent.HingeConstraint.TargetAngle - 6
    loop = math.random(1,10)
end

So despite your lack of proper indentation (sorry but I must note), this end statement belongs to the inner loop, and this inner loop has a wait(1) statement inside. So that seems all fine: as long as the inner loop runs, the script won't crash.

The inner loop draws random numbers until the random number is 8 or higher, then goes through the next part and comes back to the start of the outer loop.

But when it tries to enter the inner loop for the second time, it looks at the variable loop which is declared outside all the loops and thus remembered: It finds inside that variable an integer value (number) higher than 7, and thus does not execute the loop.

Then it immediately goes to the next part:

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

But this just connects a function to an event and doesn't execute anything now. More importantly, it has no wait statements and no way to break out of the loop.

It's not a good idea to connect infinite connections over time without closing anyway.

But the point is that the third time, the loop variable still didn't change, and the outer loop keeps looping really fast infinitely thus breaking the script.

I hope this helps to understand the problem. I can't tell exactly what your script is supposed to do, but I think removing the outer loop and putting the inner loop inside your function that you connect to mouseclick (once) might work better:


local CD = script.Parent -- Clickdetector CD.MouseClick:connect(function(click) local loop = math.random(1,7) --this can just be local loop=1 while loop < 8 do wait(1) CD.Parent.HingeConstraint.TargetAngle = CD.Parent.HingeConstraint.TargetAngle - 6 loop = math.random(1,10) end wait(1) end)

(similar to @tiraner300's solution. I moved the loop variable to inside the function too, so mouseclicks don't interfere with eachother, as I don't see the reason to make it the same variable for different mouseclicks. Your script may be intended to do something completely different though!)

Ad
Log in to vote
0
Answered by 6 years ago

I don't know why you want to have a loop inside a loop, this code should do what you want to accomplish without the use of a double loop:

local loop = 1
local hingeConstraint = script.Parent.Parent:WaitForChild("HingeConstraint")
local CD = script.Parent -- Clickdetector

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

while true do
    if loop < 8 do
        hingeConstraint.TargetAngle = hingeConstraint.TargetAngle - 6
        loop = math.random(1,10)
    end
    wait(1)
end
Log in to vote
0
Answered by 6 years ago

You need to add another wait to the loop.

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

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

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

Answer this question