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

How to Stop a Function from being Looped?

So I'm trying to get out of a Function that continues to run. I have tried to work around it by making an "if then end" but since it's not updating the state of that variable once the variable is "true" (let's say) it continues the function within the "if then" even when later that variable to set to "false", it doesn't read an update. I'm sure there is a way to do this but not sure.

TL;DR I'm not sure how to say "if this variable == false at ANY POINT stop running this function"

Code I'm Working With (I know it's messy oops)


-- Ignore the fact some variables are missing....don't feel like making this longer than it already is local _check = true

function mouseMove() local posX, posY, posZ = mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z Preview.Position = Vector3.new(posX,posY,posZ) print ("Im Still Running") -- THIS IS PROVING THAT FUNCTION NEVER STOPS mouse.Button1Down:connect(function() print("BOOP") ply.Items.Oof.Value = false _clone.Position = Preview.Position script.Parent.Parent.Parent.ScreenGui.MyButton["De 1"].ItemName.Text = "" _check = false Preview:Destroy() end) end

if ply.Items.Oof.Changed:connect(function() if ply.Items.Oof.Value == true then game:GetService("UserInputService").InputBegan:connect(function(_inOb,gameProcessedEvent) if _inOb.KeyCode == Enum.KeyCode.One then _check = true if _check ~= false then mouse.Move:connect(mouseMove) --Once line Get's here it runs above function every mouse movement, this is the issue. Somehow it get's here when I set _check = false end end end) end end) then end


1 answer

I have found a temporary solution by just disabling and re-enabling the script. Not a "bug fix" but a way from keeping that bug happen. Hey on the bright side the way I made the disabling and re-enabling made 1 less script run until the point it's needed.

6 comments

0

Vizikk - connect a new event in a changed event and a Move event. Duplicating events in every event ran.

0

Vizikk - "why does it run multiple times" I know lets diable and enable the script to fix it

0

kingdom5 - why not just track whan is connected and disconnect the events when they are not needed.

0

kingdom5 - or even use the _check and other variables in the functions to see if the event should run the code.

0

Use a denounce, it's simple and effective.

0

kingdom5 I'm not a Lua God. I come from a strong C++ and C# background. Doing things how I'm used to doesn't work well with Lua. It's been since golden age of GMod since I messed with Lua. That said, I completely forgot about disconnecting it. Also, RAYAN1565 I have never heard of a "denounce" within Lua. Is this a Roblox thing?

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