#Connections Question

1 messages · Page 1 of 1 (latest)

quiet lagoon
#

Hi! I have a question regarding connections. When should I attatch a variable to a connection, and when should I disconnect a connection? If you can, try and provide examples, because it is hard for me to grasp when it's necessary or not. Thanks!

sly cairn
#

If you only want to temporarily listen (as example wehn a player joins) to something and need to disconnect it afterwards you want to store it in a variable, thats pretty much it. Questions?

#

@quiet lagoon

real cloak
languid steppe
#

you'll know

#

but if you want to see an example from me I gotchu

naive raft
#

use connections when nesting connect: functions for example

local connection = nil

SWORD.Activated:Connect(function() --This is :Connect function
    --add animation and sound effects
    connection = HANDLE.touched:Connect(function(hit) --This is a NESTED :Connect function
        if hit.Parent:FindFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage()
            connection:Disconnect()
        end
    end)
end)

the reason why - :Connect functions run constantly so when you run SWORD.activated it will run HANDLE.touched, but HANDLE.touched wont stop properly after the code is finished. meaning, If i put a "print("test")" inside HANDLE.touched... without disconnecting it then it will run:
test(x2)
then
test(x4)
then
test(x8)
it will double up every time you run SWORD.activated