Help with this UserInputService KeyCode

HI,
I am having lot of trouble trying to figure out how to make this variable called temp will change to true/false when the player presses and holds down Space and presses W twice.
here is the script -

local temp = false
local uis = game.GetService("UserInputService")
uis.InputBegan:Connect(function(key,chat)
if chat then return end
if KeyCode == Enum.KeyCode.SPACE + [2x tap] W then
if temp then
temp = false
print("Changed to False")
else
temp = true 
print ("Changed to True")
end
end
end)

hope this is the script you shall need for this… :smiley:

you did not define keycode

it should not be keycode, it should be key.KeyCode

Scroll down on this link to see how it works: UserInputService | Documentation - Roblox Creator Hub

I triend but it still dosent work :frowning:

You have some serious issues with your code. I’ve written something that works here:

local UserInputService = game:GetService("UserInputService")

local spaceIsHeld = false -- check if space is currently held
local temp = false

local Time = tick()
local debounce = false
local COOLDOWN = 0.3 -- Timer for cooldown between double-taps


UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Space then
		spaceIsHeld = true -- Space is currently being held.
	elseif Input.KeyCode == Enum.KeyCode.W and spaceIsHeld then
		
		-- tick() - Time is checking to see if W has been pressed twice within the timeframe of 0.3 seconds or less,
		-- since we recorded the time it was last released in InputEnded.
		if tick() - Time < 0.3 and spaceIsHeld and not debounce then
			if temp then
				temp = false
				print("Changed to False")
			else
				temp = true 
				print ("Changed to True")
			end
			
			-- Needs a debounce or else player can just keep spamming W, and it's not really a double tap.
			debounce = true
			wait(COOLDOWN)
			debounce = false
		end
	end
end)


UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
	if Input.KeyCode == Enum.KeyCode.Space then
		spaceIsHeld = false -- Space is no longer being held.
	elseif Input.KeyCode == Enum.KeyCode.W then
		Time = tick() -- After W is released, a time is recorded. This recorded time is check against a new time in InputBegan.
	end
end)

If you have any questions, please let me know.

does the function even run? place a print function after “if chat then return end” to test it

It does run but I guess there is no way to make it SPACE + W as is said by @Hypgnosis There needs to be a function where we know when space is help down

Thanks for that I tried it and now it finally works :smiley:
I really appreciate it :+1:

2 Likes