Issue with weapon equipped and hold module

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

You can check if a tool is equipped by simply checking its Parent property and whether it’s parented under the character (equipped) or the backpack (unequipped).

i should have clarified, what i mean by equipped isn’t the sword being held out, if the player presses mouse button 1 while sword is held out the sword becomes equipped, the equipped function runs correctly, not show how I will change the if statement in the hold weapon function though

Is it’s in the character model, it’s equipped.
Is it’s in the backpack, it’s not equipped.

something like this…

if player.Backpack:FindFirstChild(tool) ~= nil then
	equipped = false
elseif player.Character:FindFirstChild(tool) ~= nil then
	equipped = true
end

Could you show the script output?

image
so i held out the weapon the it runs:
print(“Holding weapon:”, WeaponName)
next i press mouse button 1 and it runs:
print(“Equipped weapon:”, WeaponName)
like intended now i unheld the weapon and it runs
print(“Unheld weapon:”, WeaponName)
this script isnt what i wanted, because the weapon is now equiped its ment to run
print(“Unholding equipped weapon:”, WeaponName)
same issue with holding out function

Could you print equippedId and Id?

image
it prints the correct values, the 225173 is the generated id for the weapon while 1 is the players attribute equiped Id.

I believe that’s part of the issue then, they aren’t the same.

your right idk how i missed that, Thanks!

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