Why is my game lagging when i equip a tool?

Im trying to make a custom inventory that equips items when you click 1…7. But when i equip the tools it causes my game to freeze for about 2 seconds then levels out at 15 fps. (before equiping its about 60)

function equip(key)
	for i,v in pairs(player.Backpack:GetChildren()) do
		if v.INFO.Slot.Value == key then
			Humanoid:EquipTool(v)
			print(v.Name)
		end
	end
end
function starting(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode.Value <= 55 and input.KeyCode.Value >= 48 then
		if input.KeyCode.Value ~= last then
			Humanoid:UnequipTools()
			current = nil
		end
		last = input.KeyCode.Value
		if current == 1 then
			Humanoid:UnequipTools()
			current = nil
		elseif current == nil then
			Humanoid:UnequipTools()
			current = 1
			equip(input.KeyCode.Name:lower())     
		end
	end
end
userinputser.InputBegan:Connect(starting)

Why is it lagging? when i equip just a tool with only a handle there is no lag. The tool that i equip that makes the lag works without lag on just a empty baseplate. Thanks for your help :slight_smile:

Hello!

From what you’ve told us, it’s hard to know what could be causing the lag. It could be the tools itself. Does it spawn a lot of particles? Does it’s script perform any heavy tasks? Is it raising any errors? Is it printing out a lot of values (rapid and fast use of print() is performance demanding)? You’ll have to tell us more.

I’ve changed your code a little bit. input.KeyCode.Value doesn’t exist. Another thing I’ve noticed is that you are trying to compare number data type and enum data type. Enumerations are not numbers, so that isn’t possible.

A better approach is to store all the enums in a table (I chose dictionary-type) as indexes, and have their values represent the numbers. All that is happening is conversion.

local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local backpack = Players.LocalPlayer.Backpack:GetChildren()

--[[
	Dictionaries are super fast and efficient. input.KeyCode is
	enumeration type of data and represents an index here.
]]
local CONVERSION = {
	[Enum.KeyCode.One] = 1;
	[Enum.KeyCode.Two] = 2;
	[Enum.KeyCode.Three] = 3;
	[Enum.KeyCode.Four] = 4;
	[Enum.KeyCode.Five] = 5;
	[Enum.KeyCode.Six] = 6;
	[Enum.KeyCode.Seven] = 7;
}

--[[
	Alternatively, we could store all these keycodes and items
	in the same dictionary-type table.
]]
local items = {}

local last = nil

--[[
	Firstly, register all present tools in the backpack.
	Later on, if the player picks up a new tool, you should
	make a separate function that adds it in.
	
	What if the player decides to drop the item?
	
	local function removeTool(tool)
		local index = table.find(items, tool) -- find tool's index
		table.remove(items, index) -- free up given "spot"
	end
]]
for i, v in pairs(backpack) do
	table.insert(items, v)
end

local function equip(key)
	if (items[key]) then
		humanoid:EquipTool(items[key])
		print(items[key].Name)
	end
end

local function inputStart(input, gameProcessed)
	if (gameProcessed) then return; end
	if (input.UserInputType == Enum.UserInputType.Keyboard and CONVERSION[input.KeyCode]) then
		local key = CONVERSION[input.KeyCode]
		
		if (key == last) then
			humanoid:UnequipTools()
			last = nil
		else
			equip(key)
			last = key
		end
	end
end

UIS.InputBegan:Connect(inputStart)