Equiping sword bugs out

so the weapon equip script used to work, but then I changed some stuff around using attributes, now whenever I press e it equips the sword but when I press e again, it unequips the sword then immediately reequips it

local chr = player.Character
local hum = chr:WaitForChild("Humanoid")
local module = require(game:GetService("ReplicatedStorage").Modules.WeaponModule)
local Name = script.KatanaName.Value
local SwordEquip = false 
local debounce = false
	
	
uis.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.E  and chr:GetAttribute("HasSword") == true then
		if chr:GetAttribute("SwordEquip") == true  and debounce == false then
			debounce = true
			local WeaponUnEquipAnim = game:GetService("ReplicatedFirst").Animations.KatanaUnEquip
			local loadUnEquipAnim = hum:LoadAnimation(WeaponUnEquipAnim)
			loadUnEquipAnim:Play()
			loadUnEquipAnim:GetMarkerReachedSignal("KatanaUnequipEvent"):connect(function()
				module.WeaponUnEquip(player,Name)
			end)
			task.wait(1)
			chr:SetAttribute("SwordEquip",false)
			debounce = false
		end

		if chr:GetAttribute("SwordEquip") == false and debounce == false then
			debounce = true
			print("this work?")
			local WeaponEquipAnim = game:GetService("ReplicatedFirst").Animations.KatanaEquip
			local loadEquipAnim = hum:LoadAnimation(WeaponEquipAnim)
			loadEquipAnim:Play()
			loadEquipAnim:GetMarkerReachedSignal("SpawnSwordEvent"):connect(function()
				module.WeaponEquip(player,Name)
			end)
			local WeaponIdleAnim = game:GetService("ReplicatedFirst").Animations.KatanaIdle
			local loadIdleAnim = hum:LoadAnimation(WeaponIdleAnim)
			loadIdleAnim:Play()
			task.wait(1)
			chr:SetAttribute("SwordEquip", true)
			debounce = false
		end
	end
end)

Try setting the debounce to false after the both of the if statements:

if chr:GetAttribute("SwordEquip") == true and debounce == false then
	-- unequip stuff here
end

if chr:GetAttribute("SwordEquip") == false and debounce == false then
	-- equip stuff here
end

debounce = false
1 Like

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