When I press a certain button, boolean is false when it's not supposed to be?

What do you want to achieve?
I’m making a sword and shield tool where if the player presses LeftShift and “equipped” is false, then the player equips the tool which then sets “equipped” to true. When the player press LeftShift and “equipped” is true then the player draws their shield.

What is the issue?
I equipped the tool and printed out “equipped”. It’s true, as it should be. But when I press LeftShift and print out “equipped”, it says it’s false.

local equipped = false

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)


UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return
	end
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if not equipped then
			tool.Parent = character
			equipEvent:FireServer("Equip")
		else
			guardEvent:FireServer("Guard")
		end
	end
end)

I cut out some parts that I don’t think are relevant. If you need me to post the full script, I can do that for you.

What solutions have you tried so far?
I checked my script like 10 times over to see if I accidentally wrote somewhere to set “equipped” to false but the only function that changed the boolean was paired to the Tool.Equipped and Unequipped signals. I set prints everywhere throughout my script to see what was the issue but like I said before, “equipped” is only false when I press LeftShift while everything else says it’s true.

equipEvent.OnServerEvent:Connect(function(plr, bool)
	local humanoid = plr.Character.Humanoid
	if bool == "Equip" then
		tool = plr.Character
		humanoid:AddAccessory(swordClone)
		humanoid:AddAccessory(shieldClone)
	else
		swordClone.Parent = nil
		shieldClone.Parent = nil
	end
end)

guardEvent.OnServerEvent:Connect(function(plr,bool)
	local humanoid = plr.Character.Humanoid
	local guardAnim = humanoid:LoadAnimation(animations:FindFirstChild("Guard"))
	if bool == "Guard" then
		attacking = true
		guardAnim:Play()
		guardAnim:GetMarkerReachedSignal("Guard"):Connect(function()
			humanoid.Guard.Value = true
			guardAnim:AdjustSpeed(0)
		end)
	else
		attacking = false
		humanoid.Guard.Value = false
		guardAnim:Stop()
		guardAnim:AdjustSpeed(1)
	end
end)
1 Like

Could you post the code for your remote events (equipEvent and guardEvent)?

1 Like

Yes, I’ve edited them in for you

1 Like

I just found out that the Equipped and Unequipped signals only fire when the tool has a handle. I could’ve sworn they were working when I tested the code but I guess not.

1 Like