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

How do i get the wait time and pause/unpause an animation while a key is being held?

Im trying to make an animation slow down, or loop the first few seconds, when a key is being held;but, i also want the animation to play when the key is no longer being held. In addition, i want the time that the key was being held.(This might be too much to ask for, but please help me) Here is the script.

mouse.KeyDown:connect(function(key)
	local player = game.Players.LocalPlayer
	local Humanoid = player.Character.Humanoid
if key == First and CD == "No" then
	local StrongPunch = script:WaitForChild("StrongPunch")
	local animTrack = Humanoid:LoadAnimation(StrongPunch)	
	animTrack:Play()
CD = "No"
	end

end)
end)

	

1 answer

✓ Accepted answer

Check this out for the info on Keyboard input: http://wiki.roblox.com/index.php?title=Keyboard_input

As for the pausing and unpausing, I can't be very much help. But I can show you how to fire a function when a key is being held, and get the wait time.

Here is the basic structure for getting the keyboard input:

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then--If the key was R then...
		print("Player pressed R")
	end
end
 function onKeyOff(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then--If the key was R then...
		print("Player stopped pressing R")
	end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)--This fires when a key has been pressed.
game:GetService("UserInputService").InputEnded:connect(onKeyOff)--This fires when a key has stopped being pressed.

As for finding how long a player has pressed R, or whatever key you want, you can simply put a loop in the function that fires when R has been pressed (onKeyPress), and in that loop you can count each second that has passed. When the key has stopped being pressed, the function that fires when R has stopped being pressed will make the loop stop.

Here's the visual:

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then--If the key was R then...
		print("Player pressed R")
		Pressing = true--The function has just fired, so Pressing should be true
		Counter = 0--The time starts at zero
		if Pressing then--Start the loop(This won't stop until Pressing is false)
			wait(1)
			Counter = Counter + 1--After every second, the counter gains 1
		end
	end
end
 function onKeyOff(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then--If the key was R then...
		print("Player stopped pressing R")
		Pressing = false--The Player has stopped pressing R, so stop the loop.
		print(Counter)--The amount of time the Player was pressing!
	end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)--This fires when a key has been pressed.
game:GetService("UserInputService").InputEnded:connect(onKeyOff)--This fires when a key has stopped being pressed.

The Counter variable can only count every second. If you want to count in fractions of a second, then simply lower the wait(1) and Counter + 1 accordingly.

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