Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my script unequip the tool even though i havent pressed the key twice?

The sword automatically unequips itself after the debounce has ended

The local script

local Pressed = false
local Debounce = false

local function PressedKey()
if Pressed == false and Debounce == false then	
	print("KeyPressed")
	Pressed = true
	Humanoid:EquipTool(Sword)
	Debounce = true
	wait(5)
	Debounce = false
	end
	
if Pressed == true and Debounce == false then
	print("Pressed Again")
	Pressed = false
	Humanoid:UnequipTools(Sword)
	Debounce = true
	wait(5)
	Debounce = false
end	
end

ContextActionService:BindAction("KeyHasBeenPressed" , PressedKey, false, "z")

2 answers

✓ Accepted answer

I suggest you use UserInputService since ContextActionService fires on InputBegan and InputEnded. That means that the function KeyPressed will be called two times; once when you press the key, and once when you release the key.

I also made a setDebounce function to reduce the boilerplate code. This should work exactly as you've inteded it to work.

The variable Pressed was also unnecessary. You only need one if statement with one Debounce variable.

You were setting the debounce variable inside the first if statement, but when the 5 seconds wait was over the compiler would go line to line and execute the next if statement in the same function call. Using 1 if statement fixed this problem.

if not debounce then  
    humanoid:EquipTool(sword)
    setDebounce()
else
    humanoid:UnequipTools(sword)
    setDebounce()
end

Here is the complete script.

-- Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local playersService = game:GetService("Players")
local userInputService = game:GetService("UserInputService")

-- Player
local player = playersService.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local sword = replicatedStorage:FindFirstChild("Sword")

local debounce = false

local function setDebounce()
	debounce = true
    wait(5)
	debounce = false
end

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		if not debounce then  
		    humanoid:EquipTool(sword)
		    setDebounce()
		else
		    humanoid:UnequipTools(sword)
		    setDebounce()
		end
	end
end)

Please feel free to ask any questions you might have about my solution, and how I came up with it.

1 comment

0

Thanks dude!

Because it says in the first if statement to set Pressed true and after the 5 second wait timer it turns it to false the debounce so the second if statement fires.

2 comments

0

so how do i fix this?

0

well depends if you want to wait a certain amount of time to unequip it or maybe when you do a certain action like if(slash) then debounce = false like that or make the wait time longer or change the statement for the second if statement

This discussion is preserved as it was. New answers, comments, and votes are closed.