Hello, so I’ve been browsing the forums trying to understand rbxscriptconnections. My question is does my code here cause too many connections to be created if a user is to press E constantly?
Essentially, does each time a user presses E and fires this remote event, does it just keep adding connections and stacking up on them each time “E” is pressed and thus consuming memory?
Or would Roblox just keep deleting and replacing the connection each time “E” is pressed?
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local PressE = ReplicatedStorage:WaitForChild(“PressE”)
PressE.OnServerEvent:Connect(function()
print(“connection established”)
end)
Short answer no, however let’s say you added a touched event in side that remote event connection to a part that will stack up every time, in that case you should store the touch connection in a variable and if it’s not nil then Disconnect it.
Here’s an example of what i was talking about:
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local PressE = ReplicatedStorage:WaitForChild(“PressE”)
local touch
PressE.OnServerEvent:Connect(function()
print(“connection established”)
if touch then touch:Disconnect() end
touch = game.Workspace.Part.Touched:Connect(function(hit)
print("Since I disconnect this Everytime before the connection, this part will have only one touch connection")
end)
end)
See here, Since I disconnect this Everytime before the connection, this part will have only one touch connection
The ‘Connect()’ instance method of RBXScriptSignal objects (events) returns a single RBXScriptConnection object (connection), as the method is only called once, only a single connection is made.