How to stop a script when something is added?

What I’m trying to do:

I’m trying to stop the code which goes after and outside the whole hum.ChildAdded:connect(function(Child) function from running if a child is added, how would I do this? I’ve already considered doing something like this:

local interrupted = false
hum.ChildAdded:connect(function(Child)
		-- if find value then interrupted = true end

	end)

But this means I would need to put if statements to check if interrupted is true or not every few lines and there is probably a way better way to stop a script than to check if a value is true and do a return.

Here is a part of the script that I have already made:

local Anim = AnimF[combo]
	Anim:Play()
	
hum.ChildAdded:connect(function(Child) -- detect if a child is added to the humanoid
	if check(char, "Mid") then -- a function which checks for certain bool values in the humanoid and if there are returns true
		Anim:Stop()
		return -- this is where the code for stopping the script should go

	end
end)

-- rest of the script goes on
	

All help is appreciated!

Return won’t stop it in this case, only for the time it ran, put in the future when a child gets added it will still run, what you’d have to do is disconnect the event.

Example:

local Anim = AnimF[combo]
	Anim:Play()
	
local con = hum.ChildAdded:connect(function(Child) -- detect if a child is added to the humanoid
	if check(char, "Mid") then -- a function which checks for certain bool values in the humanoid and if there are returns true
		Anim:Stop()
		con:Disconnect() -- this is where the code for stopping the script should go

	end
end)

-- rest of the script goes on

Maybe you looking for

local a = hum.ChildAdded:Connect(function(child)
-- Do if checks, after that
        a:Disconnect()
end)

that doesn’t even work?

Also the point is not the stop the childadded:connect but the rest of the script that goes after it

You must define the variable globally:

local con 
con = hum.ChildAdded:Connect(function(Child)
--rest of the code

PS: To stop the script just disable it(script.Disabled = true)

Sorry I didn’t specify but it’s a module script, used by every single player, I’m just trying to stop the code that goes on outside of the hum.ChildAdded:Connect(function(Child) and nothing else, I’m not trying to disable to whole thing as that wouldn’t work in my case.

Oh just to clarify I’m going to make an example of what I want to achieve:

local module = {}

function module.Example(hum)

  hum.ChildAdded:connect(function(Child)
  	-- if the child's name is the same as one from a list then
        --stop the script
  end)
  
  wait(5)
  print("This line has been reached!!!")
  
end

return module

In this example lets assume a value from the said list is added 2 seconds after the module has started, the hum.ChildAdded:connect(function(Child) should detect this and then stop the script, the stopping of the code should prevent the code from reaching the line with the print and so it’s not printed, this is what I’m trying to achieve and I’m just looking for the code which would go inside the hum.ChildAdded:connect(function(Child) which does the stopping.

Ok, before I kill my brain trying to help you, do you want just that function in the module to pause, or the whole module

I just want this to stop from running, but to be able to be called/run again afterwards so not completely disabled:

function module.Example(hum)

  hum.ChildAdded:connect(function(Child)
  	-- if the child's name is the same as one from a list then
        --stop the script
  end)
  
  wait(5)
  print("This line has been reached!!!")
  
end

I don’t think you can do anything other than an if statement.


function module.Example(hum)
    local childNames = {}
    hum.ChildAdded:connect(function(Child)
          if table.find(childNames, Child.Name) then interrupted = true else interrupted = false end
        --stop the script
    end)
   if not interrupted then
      wait(5)
      print("This line has been reached!!!")
    end 
  
end

Awww, well that sucks as I have quite a long script which would need stopping/checking at multiple points. Thanks for the help anyways!

Actually, there might be another way. Try something like this:

function module.Example(hum)
    local childNames = {}
    task.spawn(function()
        hum.ChildAdded:connect(function(Child)
              if table.find(childNames, Child.Name) then interrupted = true else interrupted = false end
            --stop the script
        end)
    end)
   repeat task.wait() until not interrupted
      wait(5)
      print("This line has been reached!!!")
    end 
end

This in theory should work, however, it might not be the greatest for performance.

Not ‘globally’ but as a non-local or ‘upvalue’.

1 Like
local State = true


local function Function()
	local Connection
	local function OnChildAdded(Child)
		Connection:Disconnect()
		--Perform conditional check on 'Child.'
		State = false --Disable the flag.
	end

	Connection = Humanoid.ChildAdded:Connect(OnChildAdded) --Humanoid will need to be defined.
	task.wait(5) --Wait for some length of time.
	if State then --If no child has been added.
		
	else --If one or more children have been added.
		
	end
end

You could use a state variable, value object or an attribute to track state.

1 Like