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
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
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.
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
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.
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.