But now to get back to what the real issue of the code is here. Starting a loop that has a delay between checks like this and stopping it at the press of a button is a bit more complicated and a boolean debounce can create issues if it’s flipped 2x before the loop can check it again (in this case it only checks the condition in the while loop every 2 seconds)
The way I got around this in my code is actually storing the tick the loop was started at and comparing it to that. That way upon the check it will realize it’s incorrect. That’s essentially what this code is doing before entering the while loop
fireworksEnabled = not fireworksEnabled
newLoopTime = tick()
local loopTime = newLoopTime --This is all setup to allow the loop to only continue on the last hit
That way if you call the function again, the currently running loop will recognize there is an updated loop and stop itself.
That is the hidden issue in your code. The issue that prevented it from working at all though is actually just a simple mistake.
script.Parent.MouseButton1Down:Connect(function()
Firing.Value = true
while Firing do
Fire()
if not Firing then
break
end
end
end)
You are only checking if Firing exists. It does exist as a child of workspace so you are saying something no different than true when you just type Firing. You should be checking Firing.Value.
while Firing do --should be while Firing.Value do
if not Firing then --should be if not Firing.Value then
You can do this with the code you originally have. This is just a suggestion, but I believe you can use coroutines to solve your problem. First, you should make your Fire function a coroutine by doing local fire = coroutine.create(function(). I recommend that the code below the coroutine be already a while loop. With that the function is not running, so you will have to put coroutine.resume(fire) somewhere in your script. In your mouse button 1 down event, you can say after Firing.Value = true, if Firing do coroutine.resume(fire)
and then elseif not Firing then coroutine.yield(fire)
Once again this is just a suggestion and I am not sure if this will work.
Hi there! I believe the easiest option to do here is just a break function. I believe this to be so as the loop was essentially never told to stop after the button was first pressed, putting a break in the loop at the end would stop it until the button is pressed once more.
Hey, sorry for the late answer, I had a bit of a busy 2 days lol.
You have no idea how much I appreciated your answers! The explanations were pretty clear and helped me a ton to understand more about loops and tables. Seriously, thank you!