|
| 05 Jan 2016 05:40 AM |
What I'm asking for exactly is if there is a way to stop an Event that's running completely. For example, if I were to create a function with a Touched event, is there a way that I can stop that Touched event from happening? I want it to happen only once, without removing the part.
I know that you can use booleans to check if it's happened before, but then the Event is still running in the background. Every time it would touch something, it would still run through the function, even though the boolean tells it that it's already been used.
For one more example, I'll create an example code of what I'm talking about, so you can picture what I mean:
script.Parent.Touched:connect(function(hit) if hit.Name == "Right Leg" then print("Ending function") -- disconnect event so that it never runs again end end)
-- Other important script stuff
Thank you for taking the time to read this!
|
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 05:43 AM |
listen = event:connect(f)
listen:disconnect() |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 05:48 AM |
| Oh, does that work? The wiki says that it cannot be used anymore, that it's obsolete. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 05:49 AM |
| Not 100% on what you're getting at, but it sounds like you can just debounce. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 05:50 AM |
script.Parent.Touched:connect(function(hit) if hit.Name == "Right Leg" and deb == false then print("Ending function") deb = true end end)
-- Other important script stuff
Simple enough |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 05:51 AM |
| The wiki says that you can't disconnect events anymore, but you can disconnect listeners |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 05:54 AM |
the wiki page just stupidly wrote that
what i think it means is disconnect is to be used with connections instead of events
example:
local Event = script.Parent.Touched local Connection = Event:connect(function() end)
Connection:disconnect() |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 05 Jan 2016 05:55 AM |
local touched
touched = script.Parent.Touched:connect(function(hit) if hit.Name == "Right Leg" then print("Ending function") touched:disconnect() end end)
|
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 05:59 AM |
@Sunstaff, I didn't want to use debounce because it still runs through the function every time it touches something, and I never planned on using the function again after it touched a particular part, so the problem is that the function will unnecessarily exist. And while that doesn't necessarily cause too much lag, my computer has hard enough time running Studio after I start getting anywhere that matters, so every little bit of lag adds up, and I guess I'd like my game to be optimized best that I can get it.
Anyway, so what you guys are saying is that the point from the wiki was that I can't script.Parent.Touched:disconnect(), but I can disconnect the connection itself? That makes sense, but it also kind of seems obvious. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:01 AM |
| Disconnect would be the best and really only route then. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:02 AM |
Oh, chimmi, brilliant! I was getting ready to post that I had a problem with the code that I wrote, which was pretty much identical to yours, except I didn't have:
local x
x = script.Parent.Touched:connect()
and instead, I had:
local x = script.Parent.Touched:connect()
and it wasn't working. Why did that make the difference? |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 05 Jan 2016 06:06 AM |
Because the connection variable had not been defined when the function was created, so it was nil. Predefining it will fix that problem.
|
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:07 AM |
| Because Lua can't just make sense.... |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:11 AM |
| Alright, well thanks all! I think I can figure out what I need from this. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:15 AM |
| glicon u bully u didnt acknowledge my explanation :( |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:16 AM |
Stop worrying about these 'little bits of lag', one function won't cause anything more than .01% script performance and that's only while it runs. You could use a debounce, and be just fine.
Lua compiles efficiently enough. Your code is good enough. Just don't intentionally make it bad.
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:18 AM |
| @UnstableScript0 debounces are dumb when you can just completely disconnect the event. little bits of lag should be avoided because they can become more laggy the more "little bits of lag" occur |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:21 AM |
Sigh you idiot, you completely missed my point. I was saying it WOULD be fine. What I am saying is 1 little function existing will cause no lag, and to stop worrying about it. Lua compiles efficiently. I do not need you to tell me how to script.
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:30 AM |
Actually climaxius, this whole paragraph was pretty much directed to you:
"Anyway, so what you guys are saying is that the point from the wiki was that I can't script.Parent.Touched:disconnect(), but I can disconnect the connection itself? That makes sense, but it also kind of seems obvious."
:P
But unstable, you're right. Of course one little function existing won't cause any lag. Although, that's why I made it a point to explain my horrible computer, and that I'm not just trying to make one little function. It's actually a big function, and it's not going to just be one function anyway, I plan on building a game, and trust me, lag does build up, especially on my computer.
My problem is already solved. Thanks all. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:36 AM |
If you want other ways to stop lag, try to build efficiently with no un-needed parts and unions when necessary, and preload images, and script efficiently
unstable, everything matters, no need to call me an idiot for fixing your mistake.
maybe i do need to script because if you dont pay attention to the little details, you are a terrible scripter |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 05 Jan 2016 06:38 AM |
"What I am saying is 1 little function existing will cause no lag"
The function itself would be taking up precious memory, and while function calls alone are not super expensive they do build up. If you were to have a script that detects one touch and does something with it using a debounce instead of disconnection it in a lot of parts it could be bad.
|
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:39 AM |
@chimmihc and he calls me an idiot for telling him there are more efficient ways to go about things.
chimmihc, please beat him up for me. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 02:48 PM |
I didn't need you to tell me. I knew it could be done more efficiently. What I am saying, for the third time, is one little function does little to no lag. Maybe it's my PC, but I can handle the worst of a free modeled game with no visible lag.
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:13 PM |
| @UnstableScript0 and by saying so you everyone thought you were saying not to change his code to be better, and not to use connection:disconnect(). if so, youre wrong and thats why we spoke up |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2016 06:31 PM |
I clearly was just saying stop worrying. Stop being an idiot, and get out. I know how to script and don't need your advice, because I would have used the :disconnect() method in that case. I was just saying that 1 function won't cause lag. I don't need you to tell me when I am wrong.
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played. |
|
|
| Report Abuse |
|
|