How to know if you pressed a key the second time

If i press q i do a pose
if i press q again the pose animation will stop
how can i do that?

Consider looking at UserInputService

3 Likes

You can use UserInputService.

For your example:

--local script
local UserinputService = game:GetService("UserInputService")


UserinputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.Q then
       --do stuff
    end
end)
2 Likes

i want it to be toggable but mines not working:(
in the script it has print(“hello”) for on
and in the other it prints(“bye”) for off
it prints both
i want it to be toggable


local rem = game:GetService("ReplicatedStorage").RemoteEvent
local rem2 = game:GetService("ReplicatedStorage").RemoteEvent2
local uis = game:GetService("UserInputService")

local ison = false
local isoff = true

uis.InputBegan:Connect(function(input,istpying)
	if istpying then return end
	if input.KeyCode == Enum.KeyCode.Q then
		if ison == true then
			isoff = true
ison = false
			rem2:FireServer()
		end
	end
end)
uis.InputBegan:Connect(function(input,istyping)
	if istyping then return end
	if input.KeyCode == Enum.KeyCode.Q then
		if isoff  == true then
		
		
			ison = true
			isoff =  false
		
			rem:FireServer()
			end
	end
end)

but idk how

Instead of checking twice with 2 calls,

Make a variable( can be string or a number), and for each time the player presses “Q”,add to that value +1.

With that, you’ll be able to know how many times the player has pressed “Q”.

1 Like

I know this has already been answered. But a large part of your issue is that you created 2 uis.InputBegan functions that check for the same thing. Really you should be using one.

Also having and ison and an isoff statement is redundant. If they are always going to be opposite each other you can just do ‘not ison’ to get the value of isoff.

Basically using those 2 ideas your script can be made way more readable.

local rem = game:GetService("ReplicatedStorage").RemoteEvent
local rem2 = game:GetService("ReplicatedStorage").RemoteEvent2

local ison = false

game:GetService("UserInputService").InputBegan:Connect(function(input,istpying)
	if istpying then return end
	
	if input.KeyCode == Enum.KeyCode.Q then
		if ison then
			rem2:FireServer()
		else
			rem:FireServer()
		end
		
		ison = not ison --flips the state of the boolean.  So if it was true it's now false. if it was false it's now true.
	end
end)
1 Like