I have a tool in my starterpack . I want it so that when the tool is activated it will engage a .Touched event for the handle of the tool . This will make it so that the handle will kill anyone it touches . The only problem is that once the tool is activated , even if it is no longer activated , the .Touched event will continue to run and even if you are idel and someone steps on the tool , they will die . I want to make it so that once the tool is activated , it will activate the .Touched event , making the tool deadly , but only for a few seconds . So in other words I want to completely stop the funtion from running so that even if someone touches it , they will not die . I would prefer if you told me how to do this from the outside of the function but on the inside will also work .
you will want to disconnect your events. You can look at this article here https://developer.roblox.com/en-us/recipes/How-to-disconnect-an-event-connection
oh? never heard of that . How does it work?
thats not a good idea as it will still be running wasting memory if he disconnects the even however it wont waste memory and alsomnot run until called again.
I think the article explains it pretty well, would you mind reading it first? It can probably explain it better than I can.
The general idea is you keep a boolean variable to lock the callback code from running multiple times.
I’m not sure what you’re saying, he’s not connecting the event multiple times? He wants to prevent the callback from getting invoked. Disconnecting and connecting the event every time seems very wasteful
you will not want to use debounce as it will work but use up memory in the game when you dont need it running. You should disconnect instead which stops the event until called and frees up memeory. https://developer.roblox.com/en-us/recipes/How-to-disconnect-an-event-connection
Dont forget to mark as a solution if this works
Guys do you think you can maybe write me an example of a touched event that ends after a few seconds of being active please?
not really its efficient, if the event is not called for a certain amount of time it is sitting there wasting memory causing memory leaks which is preventable with disconnecting events. Look at this article it explains it. https://developer.roblox.com/en-us/recipes/How-to-disconnect-an-event-connection
we dont write code for you but the https://developer.roblox.com/en-us/recipes/How-to-disconnect-an-event-connection article provides one
ok ill try . Imma be afk for a couple of minutes then .
here is another one that is better for understanding if you need help
http://developer.roblox.com/en-us/articles/events
Does he need to disconnect the event? He wants the callback to be invoked every time he activates the tool, or does he only want it to activate a single time?
@GHVHJGFJDUC can you confirm?
I want it so that once the tool is activated , the .Touched event will be constantly active but only for a few seconds
he wants to activate it multiple times after the touched event but even so it still is wasting memory.
None of these articles are making sense to me . Do you think there might be a YT video?
OK:
local debounce = false
part.Touched:Connect(function(hit)
if debounce then return end
debounce = true
-- handle damage here
end)
dont do that, that is wasteful for memory
the correct way is not debouncing as it hogs memory if it runs for only a few seconds there is no need to waste it on debouncing