I want to know how I can stop these type of functions.
Im refering to these ones btw:
function kaka()
end)
local function kaka()
end)
I want to know how I can stop these type of functions.
Im refering to these ones btw:
function kaka()
end)
local function kaka()
end)
This should really have gone in #help-and-feedback:scripting-support but…
Functions only run once anyway when called so you don’t exactly need to stop them.
turn it into a thread and it can be canceled by task.cancel
function kaka()
end
local thread = task.spawn(kaka)
task.cancel(thread)
you can return end, cancel the thread, there are several ways to stop a function
with return
local function func()
if conditionToStop then return end
code
end
with
task.cancel
local thread = task.spawn(function()
end)
task.cancel(thread)
you can also use
coroutine.close
local thread = coroutine.create(function()
while task.wait(0.1) do
print('running')
end
end)
coroutine.resume(thread)
task.wait(3)
coroutine.close(thread)
Sorry but none of these seem to work…
This is the code im using and trying to stop btw
function onInspect()
if Enabled and Equipped and not Inspecting and not Reloading and not HoldDown and not Sprinting then
if Module.InspectEnabled == true then
Inspecting = true
Enabled = false
if Module.HideHotBarOnInspect then
hideHotBar()
end
if InspectingAnim then InspectingAnim:Play(nil,nil,Module.InspectAnimationSpeed)
InspectingAnim:GetMarkerReachedSignal("M2"):Once(function()
Handle.WeaponFoley.m2:Play()
end)
InspectingAnim:GetMarkerReachedSignal("M3"):Once(function()
Handle.WeaponFoley.m3:Play()
end)
InspectingAnim:GetMarkerReachedSignal("M1"):Once(function()
Handle.WeaponFoley.m1:Play()
end)
InspectingAnim:GetMarkerReachedSignal("D"):Once(function()
Handle.Draw:Play()
end)
end
wait(Module.InspectTime)
if InspectingAnim and InspectingAnim.IsPlaying then InspectingAnim:Stop() end
if Module.HideHotBarOnInspect then
showHotBar()
end
Inspecting = false
Enabled = true
end
end
end
which specifies part of this code, which you want to stop?
I want to stop everything in the function, especially the wait()
one.
just put:
if true then return end
as the first line, within the scope of the function.
like this? (sorry for posting code as an image admins)
yes, when you return something, the code below is unusable. he will not be executed
Well nothing seemed to work…
I used the task.cancel method and nothing worked
Cancelling a thread will not undo any operations made. Other threads and connections you have made on a cancel(lable) thread will not be cancelled and disconnected respectively. Those will also need to be disconnected and cancelled, any any animations ran must be stopped.
I have made it so the animation stops. But theres a problem.
Once I stop those animations, the function will still work and can be used, and if its triggered,
it will glitch.
Thats the reason of my post.
Disconnections my friend. Connecting an RBXScriptSignal
will return an RBXScriptConnection
which you can store and disconnect at your cancel inspect function. You can use cleanup modules like Trove and call the cleanup function to cancel threads and disconnect connections, or manually do it yourself.
Here’s an example of what I mean. Below, I’ve linked my custom cleanup module called “Bin” if you would like to use it. Otherwise, you can use other modules like Trove.
--!strict
local Bin = {}
local BinProxy: any = newproxy()
export type BinItem = {
_function: ((...any) -> ...any),
_arguments: {any}
}
export type BinClass = {
Add: <A..., R...>(self: Bin, cleanupFunction: (A...) -> R..., A...) -> (),
AddConnection: (self: Bin, rbxScriptConnection: RBXScriptConnection) -> (),
AddThread: (self: Bin, thread: thread, pcallWrap: boolean?) -> (),
GetLength: (self: Bin) -> number,
Cleanup: (self: Bin) -> ()
}
export type BinMetatable = {
_internal: {
running: boolean,
items: {BinItem},
},
_proxy: any,
__newindex: (self: Bin, index: any, value: any) -> (),
__tostring: (self: Bin) -> string
}
export type Bin = typeof(setmetatable({} :: BinClass, {} :: BinMetatable))
function Bin.new(): Bin
local binClass: BinClass = {
Add = function<A..., R...>(bin: Bin, cleanupFunction: (A...) -> R..., ...: A...): ()
if Bin.is(bin) == false then
error(`expected to call with ':' not '.'`, 2)
end
if typeof(cleanupFunction) ~= "function" then
error(`invalid argument 'cleanupFunction' (function expected, got {typeof(cleanupFunction)})`, 2)
end
local binMetatable: BinMetatable = getmetatable(bin)
local binItem: BinItem = {
_function = cleanupFunction,
_arguments = table.pack(...)
}
table.insert(binMetatable._internal.items, binItem)
end,
AddConnection = function(bin: Bin, rbxScriptConnection: RBXScriptConnection): ()
if Bin.is(bin) == false then
error(`expected to call with ':' not '.'`, 2)
end
if typeof(rbxScriptConnection) ~= "RBXScriptConnection" then
error(`invalid argument 'rbxScriptConnection' (RBXScriptConnection expected, got {typeof(rbxScriptConnection)})`, 2)
end
bin:Add(rbxScriptConnection.Disconnect, rbxScriptConnection)
end,
AddThread = function(bin: Bin, thread: thread, pcallWrap: boolean?): ()
if Bin.is(bin) == false then
error(`expected to call with ':' not '.'`, 2)
end
if typeof(thread) ~= "thread" then
error(`invalid argument 'thread' (thread expected, got {typeof(thread)})`, 2)
end
if pcallWrap ~= nil and typeof(pcallWrap) ~= "boolean" then
error(`invalid argument 'pcallWrap' (boolean expected, got {typeof(pcallWrap)})`, 2)
end
if pcallWrap == nil or pcallWrap == true then
bin:Add(pcall, task.cancel, thread)
else
bin:Add(task.cancel, thread)
end
end,
GetLength = function(bin: Bin): number
if Bin.is(bin) == false then
error(`expected to call with ':' not '.'`, 2)
end
local binMetatable: BinMetatable = getmetatable(bin)
return #binMetatable._internal.items
end,
Cleanup = function(bin: Bin): ()
if Bin.is(bin) == false then
error(`expected to call with ':' not '.'`, 2)
end
local binMetatable: BinMetatable = getmetatable(bin)
if binMetatable._internal.running == false and #binMetatable._internal.items > 0 then
binMetatable._internal.running = true
local nextBinItem: BinItem? = table.remove(binMetatable._internal.items, 1)
while nextBinItem ~= nil do
nextBinItem._function(table.unpack(nextBinItem._arguments))
nextBinItem = table.remove(binMetatable._internal.items, 1)
end
binMetatable._internal.running = false
end
end,
}
local binMetatable: BinMetatable = {
_internal = {
running = false,
items = {}
},
_proxy = BinProxy,
__newindex = function(_: Bin, index: any): ()
error(`{index} cannot be assigned to`, 2)
end,
__tostring = function(): string
return `Bin`
end
}
return table.freeze(setmetatable(binClass, table.freeze(binMetatable)))
end
function Bin.is(any: any): boolean
return getmetatable(any) ~= nil and (getmetatable(any) :: BinMetatable)._proxy == BinProxy
end
return table.freeze(Bin)
--!strict
local Bin = require(path_to_bin)
type Bin = Bin.Bin
local partBin: Bin = Bin.new()
local function someAsyncFunction(somePart: BasePart): ()
task.spawn(function(): ()
-- add current thread to bin
partBin:AddThread(coroutine.running())
-- add touched connection to bin
partBin:AddConnection(somePart.Touched:Connect(function(partHit: BasePart): ()
print(partHit.Name)
end))
local i: number = 1
while true do
print(i, somePart:GetFullName())
i += 1
task.wait(1)
end
end)
end
someAsyncFunction(workspace.Part)
-- clear bin after 10 seconds
task.wait(10)
partBin:Cleanup()
Is the if statement to ignore warnings???