Whats the difference from a function and a connection Ex: a function that prints hi and Runservice.Renderstepped connection? They are both functions yet you can only use :Disconnect on connections.
A “connection” is what is returned from the Roblox API, once you have called :Connect on an event. It’s not the same as a function. It is a DataType that allows you to Disconnect the function you passed from the event you connected to.
Functions are not the same as connections! Functions are their own datatype, similar to how numbers, strings, and so on, are their own datatypes. Roblox API brings in its own datatypes, like Instance, Vector3, and ect. Functions can listen for events.
A RBXScriptConnection
object is returned by event:Connect
, which has a Connected
property as well as a :Disconnect
function. the Connected
property determines if the RBXScriptConnection is connected. :Disconnect
disconnects a connection. Should have been self explanatory.
Say you have a function listening for the Touched
event. It will print the name of the part that touches. But you decided you don’t want this to occur anymore after it has been touched 5 times.
You might write something like this:
local timesTouched, debounce = 0, false;
local connection; --// "forward declaring" so that this is available as an upvalue in the listener
connection = script.Parent.Touched:Connect(function(part)
if (debounce) then
return;
end
if (timesTouched > 4) then
connection:Disconnect(); --// function won't be called anymore
end
debounce = true;
timesTouched = timesTouched + 1;
wait(1)
debounce = false;
end);
To be clear, a function can be that:
local function PrintAddOf(a,b)
print("Hello! I'm an adding function!")
print("So you asked to print the result of "..a.."+"..b.." ?")
print("The answer is "..a+b)
end
And a connection is a special object returned by the Connect method, but here the function is something (like strings and numbers) that can be called by the Connect method
Button.MouseButton1Click:Connect(PrintAddOf(6, 20))
I tried to write a bit more detailed explaination but it came out too long so I’ll simplify it.
Let’s take a look at this example of an event connection:
local function onTouched(hit)
print(hit)
end
part.Touched:Connect(onTouched)
What this code does is creates a function onTouched
and then connects it to the Touched
event.
Connecting a function to an event means that roblox will call the function each time the event occurs.
In other words, roblox will be like “Oh look! the part was touched! I have to tell that to all the functions that are waiting for this information!”
Events on their own aren’t similar to functions. They can’t be called, they don’t contain code, etc. Events are something you subscribe to.
There’s already a bunch of technical explanations above so I won’t parrot them. Instead, I’ll give you something simple.
Think of them as literal terms. Connections connect a function to a signal that runs when a condition is met. A connection holds a function. Disconnecting the connection stops the function from running when the signal is fired.
Connections and functions are two distinct items, though they work hand-in-hand for an event-driven environment, among other cases.
That won’t work because you’re connecting a function after you call it. You need to pass the function itself, not call it within the Connect. You can fix this by making a function to call PrintAddOf with those arguments, like this:
local function addToTwentySix()
PrintAddOf(6,20)
end
something.Event:Connect(addToTwentySix)