Why won't my tool equip the FIRST time when the function is called?

I’m trying to make a custom inventory system, my tool doesn’t equip the first time when its called however, it equips the second time.

function Inventory:EquipTool(player: Player, itemName: string)
	print("equip tool function")

	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:FindFirstChildOfClass("Humanoid") or character:WaitForChild("Humanoid")

	local toolToEquip = player.Backpack:FindFirstChild(itemName)

	if toolToEquip then
		humanoid:UnequipTools()
		
		task.defer(function()
			task.wait(0.2)
			if toolToEquip.Parent == player.Backpack then
				humanoid:EquipTool(toolToEquip)
			else
				warn("Tool was moved before equipping:", itemName)
			end
		end)
	else
		warn("Tool not found or invalid:", itemName)
	end
end

Since the function tirggers it means script should be fine, but its likely that if you test the script too early in playtest some assets didnt get loaded yet, try checking if that is the issue

Backpack takes time to load
use in both your ReplicatedFirst and StarterPlayer.StarterPlayerScripts folders

task.wait(1)
if not game:IsLoaded() then
  game.Loaded:Wait()
end

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