#Connections Question
1 messages · Page 1 of 1 (latest)
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
You should only make a local connection variable if you need to disconnect it / stop it from running later, if you only need to run a connection once you can use :Once instead of :Connect
You would come to that situation eventually
you'll know
but if you want to see an example from me I gotchu
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