So basically i have a function inside another function lets call Them function1 and function2, How would i stop both of function2 and function1
Like the code below
local function Function1()
local function Function2()
If Condition == true then
--Stop The functions here
end
end
--Doesnt print When both functions are stopped
print("Still Running")
end
I tried using return but it doesnt work, I looked everywhere on devforum but everything seems complex Is there anyway easier than doing all of that complex stuff? Just like return
local function Function1()
local function Function2()
If Condition == true then
return
end
end
return
--Doesnt print When both functions are stopped
print("Still Running")
end
Not really sure how i can explain it in a more simple way but what i mean when stopping function2 like return or something it affects function1 too in a way that it doesnt keep running I Hope you understand what i mean
You can view this for better information, but simply
local printHello = coroutine.create(function()
print("Hello, world!")
end)
Coroutines are basically like functions that can be stopped. You can either put the entire function in the create function or just reference the function. The coroutine can be started using coroutine.resume( function name here )
local Condition = true
local function Function1()
local function Function2()
if Condition == true then
return true -- If condition is met then return true
end
end
if Function2() == true then -- If the callback from Function2() is true
print("Still Running")
else
print("Stopped Running!")
end
--Doesnt print When both functions are stopped
end
Function1() -- Start the first function
Here you go, I’ve updated this code so it should now work depending on the condition.
It works by returning a true or false value and if the value is X then it will continue in Function1.
I tried doing it using coroutines but it doesnt work because as i mentioned in the example function2 is inside function1 and function1 cant be accessed through function2 thats why it wont work i hope i this isnt confusing
local Condition = true
local function Function1()
local function Function2()
if Condition == true then
return true -- If condition is met then return true
end
end
if Function2() == true then -- If the callback from Function2() is true
print("Still Running")
else
print("Stopped Running!")
end
--Doesnt print When both functions are stopped
end
Function1() -- Start the first function
Do this:
local Condition = true
local function Function1()
if Condition == true then -- If the Condition is true
print("Still Running")
else
print("Stopped Running!")
end
--Doesnt print When both functions are stopped
end
Function1() -- Start the first function
ok let me show you something similiar to what my script looks like:
local function OnServerEvent()
Event1:Connect(function(Condition)
if Condition == true then
--End OnServerEvent Function
end)
Event2:Connect(function(Condition)
if Condition == true then
--End OnServerEvent Function
end)
Event3:Connect(function(Condition)
if Condition == true then
--End OnServerEvent Function
end
end)
end)
So the condition is inside the event thats why this doesnt work
local function OnServerEvent()
local shouldEnd = false; -- Flag to prevent multiple triggers
Event1:Connect(function(Condition)
if Condition == true and not shouldEnd then
shouldEnd = true; -- Set flag to end OnServerEvent
return; -- Stop execution for Event1
end;
end);
Event2:Connect(function(Condition)
if Condition == true and not shouldEnd then
shouldEnd = true; -- Set flag to end OnServerEvent
return; -- Stop execution for Event2
end;
end);
Event3:Connect(function(Condition)
if Condition == true and not shouldEnd then
shouldEnd = true; -- Set flag to end OnServerEvent
return; -- Stop execution for Event3
end;
end);
end;
OnServerEvent(); -- Call OnServerEvent function
Give this a go and let me know if it works for you!