Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Where is this loop going wrong? Game crashes in certain scenarios

Got it after tossing in a bunch of wait()s (and removing the else's to remove complications) now i gotta just figure out which wait() did it and why this part was causing issues. Thanks!!!

	mouse.Button1Down:Connect(function()
*****		ButtonDown = true *****
		while ButtonDown do
			if ready == true then
				local p = mouse.Target
					if p.Locked == false and (p.Position - Tool.Parent.Head.Position).Magnitude < 15 then
						ready = false
					local Data = {Part = mouse.Target, Color = ColorSelection}
					ServerControls:InvokeServer("PaintPart", Data)
					wait(1)
						ready = true
					else
					end
			 else end	 
		end	
	end)

	mouse.Button1Up:Connect(function()
		ButtonDown = false
	end)

Basically this is part of a painting script. It was going fine until I tried to add in a cooldown. The cooldown works when the player continues to hold down the mouse, but if they attempt to double click, theres an error from the starred line.

I'm just a bit confused where this is going wrong. Does anyone have advise?

Thanks!

3 comments

0

I would also advise to not use the else clause if there is nothing after it, you have alot of "else end" and it just makes the code messy, just leave it with an end :)

0

You also don't need to check if a boolean value is true by doing "if bool == true then", all you need to do is "if bool then"

0

ah ok I didnt realize. Thank you and sorry for my messiness in anything I put up here haha I've just learned from messing around with others' scripts which often arent the best

1 answer

I see many scenarios where your game would crash due to an infinite loop (loop keeps going without a wait). You have to be careful with all your conditions inside your loop.

simple solution is to put a wait before the loop ends not in a condition statement

    mouse.Button1Down:Connect(function()
*****       ButtonDown = true *****
        while ButtonDown do
            if ready == true then
                local p = mouse.Target
                    if p.Locked == false and (p.Position - Tool.Parent.Head.Position).Magnitude < 15 then
                        ready = false
                    local Data = {Part = mouse.Target, Color = ColorSelection}
                    ServerControls:InvokeServer("PaintPart", Data)
                        ready = true
                    else
                    end
             else end    
		wait(1)
        end 
    end)

    mouse.Button1Up:Connect(function()
        ButtonDown = false
    end)

Take a look at the first condition of the loop:

    mouse.Button1Down:Connect(function()
	ButtonDown = true
        while ButtonDown do
            if ready == true then
		...
		wait(1)
	    else
	    end
        end 
    end)

    mouse.Button1Up:Connect(function()
        ButtonDown = false
    end)

what if ready isn't true? the loop will still run infinitely without a wait causing it to crash. same goes for all the if statements under it, if it doesnt meet the condition the loop would just run infinitely and cause a crash

2 comments

0

Thanks for your help! My problem is that if a player clicks without holding down, the wait(1) wont help since the loop is reset when they click again. I was trying to make it so ready was added in there to only make it useable when ready was set to true again after the wait(1)

0

Oh ok I get what you mean, just make it wait(0.01) or wait() before the loop ends and wait(1) after InvokeServer :)

This discussion is preserved as it was. New answers, comments, and votes are closed.