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

6 comments
Vizikk - connect a new event in a changed event and a Move event. Duplicating events in every event ran.
Vizikk - "why does it run multiple times" I know lets diable and enable the script to fix it
kingdom5 - why not just track whan is connected and disconnect the events when they are not needed.
kingdom5 - or even use the _check and other variables in the functions to see if the event should run the code.
Use a denounce, it's simple and effective.
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?