I’m trying to create an equip module that works with my custom inventory system, everything seems to work however the script can’t differentiate if the player weapon is already equipped or not, and there’s no error msg in the output. basically,y if the weapon ID in attributes is the same as the ID of the weapon I’m trying to equip then the script knows the weapon is already equipped
local WeaponHandler = {}
local rs = game:GetService("ReplicatedStorage")
local events = rs:WaitForChild("RemoteEvents")
function WeaponHandler.HoldWeapon(player, Id, WeaponName, WeaponType)
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChildOfClass("Humanoid")
if not humanoid then return end
local equippedId = char:GetAttribute("WeaponEquipedId")
local tool
for _, v in ipairs(player.Backpack:GetChildren()) do
if v:GetAttribute("uniqueId") == Id then
tool = v
break
end
end
if not tool then
warn("HoldWeapon: tool with uniqueId", Id, "not found")
return
end
humanoid:EquipTool(tool)
if equippedId == Id then
print("Holding equipped weapon:", WeaponName)
else
print("Holding weapon:", WeaponName)
end
end
function WeaponHandler.EquipWeapon(player, Id, WeaponName, WeaponType)
local char = player.Character or player.CharacterAdded:Wait()
local weaponid = char:GetAttribute("WeaponEquipedId")
if Id == weaponid then
local m1event = events:WaitForChild("Combat"):WaitForChild("M1")
m1event:FireClient(player)
return
end
char:SetAttribute("WeaponEquipedId", Id)
char:SetAttribute("WeaponEquipedName", WeaponName)
local humanoid = char:FindFirstChildOfClass("Humanoid")
local tool = player.Backpack:FindFirstChild(WeaponName) or char:FindFirstChild(WeaponName)
if humanoid and tool then
WeaponHandler.UnholdWeapon()
humanoid:EquipTool(tool)
end
print("Equipped weapon:", WeaponName)
end
function WeaponHandler.UnholdWeapon(player, Id, WeaponName, WeaponType)
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChildOfClass("Humanoid")
if not humanoid then return end
local equippedId = char:GetAttribute("WeaponEquipedId")
if equippedId == Id then
humanoid:UnequipTools()
print("Unholding equipped weapon:", WeaponName)
else
humanoid:UnequipTools()
print("Unheld weapon:", WeaponName)
end
end
function WeaponHandler.Requirements(player, WeaponName, Id, WeaponType)
return true
end
return WeaponHandler