Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Assigning an event caller to something?

Hi, I've read a few tutorials on Roblox and Lua and I'm becoming a bit seasoned in the language. But I still need to improve

Anyways, I was looking over the classic "Pokeball" script used in the popular "Pokeball" free model on roblox, and I noticed that the Event handler was assigned to a variable.

connection = Ball.Touched:connect(onTouch)

Even more so, the variable was connected to another method, "disconnect"

...

function onTouch(hit)
	connection:disconnect() 
...

Funny thing is, this "disconnect" method is marked as obsolete on the roblox wiki (http://wiki.roblox.com/index.php?title=RBXScriptSignal#Methods), yet the model still runs.

So my main questions are,

How can use a variable to store an event handler? -Should the function still run as marked?

What ways can this variable be used?

also

Why is this obsolete :disconnect() method working and should I replace it (if so, how)?

1 answer

✓ Accepted answer

How can [you] use a variable to store an event handler? -Should the function still run as marked?

What ways can this variable be used?

Why is this obsolete :disconnect() method working and should I replace it (if so, how)?


A variable can be used to define a function. Many scripts use function Connect() which means Connect = function() as well. Connect is the name of the function no matter what in both statements. If people want to disconnect an event in the future, they will go about it as defining a variable as nil originally. Then define the variable with the connection and function below.

local Connection
Connection = game.Players.PlayerAdded:connect(function()
    print("Server must have started")
    Connection:disconnect()
end)
--If you really wanted to make a script to print when the server started, use :wait() not this method...

Since Connection is the name of the function, then you would be able to use :disconnect() to disconnect the event from the function. The Roblox wiki does state

You will soon notice that you've run into an error saying that con is a nil value. This happens because the con variable wasn't assigned before the anonymous listener was created. To get around this, all you have to do predefine the con variable:

So that is why we had to define Connection before we made the function.


As for your last question, I do not know why the Roblox wiki did that, I am assuming that the function disconnect is obsolete for RBXScriptSignal. It does state to use the RBXScriptConnection disconnect function which would be listed at the bottom of this wiki page. The function will still work, it just needs to be used on a connection line.


######If this answer helped leave an upvote, and if it solved your problem do not forget to accept the answer. If you have any more questions or you need further explanation just leave a comment on the answer and I will get back to you.

This discussion is preserved as it was. New answers, comments, and votes are closed.