UserInputservice under CharacterAdded connection, will this cause Memory Leak?

-- Located in StarterPlayerScripts
local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer

local function OnCharacterAdded(Character)
     local function OnInputBegan(Input, Chat)
          print("I won't reconnect a new thread again when Character respawns! Hopefully")
     end
     UserInputService.InputBegan:Connect(OnInputBegan)
end
Player.CharacterAdded:Connect(OnCharacterAdded)

Does anyone know? Just want to not cause a performance issue.

Couldn’t find any topic with relation to this.

Yes it will, since it’s making a new connection each time the character spawns and isn’t disconnecting it.
Just move the connection stuff outside

-- Located in StarterPlayerScripts
local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer

local function OnInputBegan(Input, Chat)
	print("I won't reconnect a new thread again when Character respawns! Hopefully")
end
UserInputService.InputBegan:Connect(OnInputBegan)

OR you can disconnect the old connection, but I don’t really recommend this as it is unnecessary

Alternative
-- Located in StarterPlayerScripts
local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Connection = nil

local function OnCharacterAdded(Character)
	if Connection then
		Connection:Disconnect()
	end

	local function OnInputBegan(Input, Chat)
		print("I won't reconnect a new thread again when Character respawns! Hopefully")
	end
	Connection = UserInputService.InputBegan:Connect(OnInputBegan)
end
Player.CharacterAdded:Connect(OnCharacterAdded)

1 Like

yeah i’ll find a way to go around this issue. I’m trying to make a xbox shiftlock keybind.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.