RBXScriptConnection

I find this a bit confusing.

local Part = script.Parent

local connection = Part.Touched:Connect(function(otherPart)
    return otherPart
end

Which will the connection in that code be, a BasePart or a RBXScriptConnection?

3 Likes

the connection is a RBXScriptConnection. lets break this down:

Part is some instance, with a bunch of RBXScriptSignals you can connect to (one of which is touched)

Part.Touched references the RBXScriptSignal

:Connect() is a method each RBXScriptSignal implements, allowing you to link a luau function to an engine event.

Connect accepts 1 parameter (the function to run), and returns one value (the rbxscriptconnection) which allows you to undo this connection if you ever wish.

The function you pass in your case:

function(otherPart)
    return otherPart
end

is called by the engine.

In this case, the return value goes nowhere. The engine is not listening for it in .Touched.

4 Likes

I see, but can we return the otherPart when the Part is touched by somehow?

3 Likes

why would you ever need that? What are you trying to accomplish?

3 Likes

You could use another function that RBXScriptSignals provide. :Wait()
In your example it would look like this:

local Part = script.Parent

local otherPart = Part.Touched:Wait()

this will then halt execution of your script until Part is touched. then the otherPart will equal to the part that touched it.

This isnt the best method all the time though because you will be unable to to checks to the part first. Another way you could do this is using a signal library or BindableEvent. in this case I used a BindableEvent

local Part = script.Parent
local Bindable = script.BindableEvent

local connection = Part.Touched:Connect(function(otherPart)
    if --[[SomeCondition]] then
        Bindable:Fire(otherPart)
end)

local otherPart = Bindable.Event:Wait()

connection:Disconnect() --Important! We need to disconnect our event to prevent memory leaks.

You should maybe read the documentation on RBXScriptSignals and RBXScriptConnections

3 Likes

Nah, it’s not very important to know bro. I just don’t know how it work.

2 Likes

Actually, I think you should set your touched connection to nil after disconnecting it (because the connection is still RBXSrciptConnection, its .Connected is just set to false)

3 Likes

Exactly! We don’t trust gargabe collector it’s meh

2 Likes

Elaborating on what @Placewith5s said. Once you dissconnect the connection theres no strong refrences to that object other then the actual refrence itself. This means that whenever you loose a refrence to it. It would then get garbage colected. You could set it to nil though if you want to clear the variable (for some reason).
But the garbagec collector will clean this up for us.

If you didnt understand any of that then here is a more simplistic example:

function foo()
    local variable = 2

    print(variable) --> 2
end

foo()
--we don't have any refrence to the variable. meaning it will be garbage collected.

This shows more about scopes. But you can see how it cleans itself up. Once we loose acess to the varaible (since its only accessable in the function) it will be garbage collected.

But if we for example do something like this:

local variable = 0

function foo()
    variable = 2

    print(variable) --> 2
end

foo()

--We still have a refrence to the variable. So it will not get garbage collected.

Its pretty hard to explain garbage collection. But it pretty much is the automatic process of cleaning stuff up that you dont have access to anymore. Of course the garbage collector isnt perfect. Things like RBXScriptConnections cant get garbage collected until they are disconnected, which is what I did in my example.

4 Likes

local connection is a RBXScriptConnection :grin:

1 Like