Tool lagging when equipped

I’m creating a tool for a melee weapon in my game. It works perfectly fine, however, if a player were to spam equip the tool, the memory usage shoots up (from a normal 300Mb to 1kMb) and lags the server.
I’ve tried putting a debounce into the .Equipped event because I thought it was the Animation loading, but it still persisted. There isn’t really anything too intensive in my code either, as far as I know.

Code: (client)


Tool.Equipped:Connect(function()
	if db == false then
		db = true
		IdleAnim:Play()
		
		Tool.Activated:Connect(function()
			if cooldown == false then
				cooldown = true
				
				UseAnim:Play()
				
				UseEvent:FireServer()
				
				wait(2.5)
				
				cooldown = false
			end
		end)
		
		db = false
	end
end)

It only happens when players equip it, not when players use it. This happens with all the other tools in my game as well. I also tried disconnecting the .Equip event, but it just stopped it from working. I’m confused on why it’s lagging so much.

1 Like

Your issue might come from the embedded connection. Give this a try and see if it works.

local activatedConnection = nil

Tool.Equipped:Connect(function()
	if db == false then
		db = true
		IdleAnim:Play()

		activatedConnection = Tool.Activated:Connect(function()
			if cooldown == false then
				cooldown = true

				UseAnim:Play()

				UseEvent:FireServer()

				wait(2.5)

				cooldown = false
			end
		end)

		db = false
	end
end)

Tool.Unequipped:Connect(function()
	if activatedConnection then
		activatedConnection:Disconnect()
		activatedConnection = nil
	end
end)

Thanks for the help, but the problem still persists. Initial memory usage is still the same in studio (1.2kmB) and the memory still spikes. I’ll try more solutions to see the problem.

1 Like

Its a default roblox issue nowdays, id reccomend making custom inventory and tool system, it will fix the issue