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

How do i use 2 of the same letters in Enum.KeyCode?

This is a script, but i dont know how to use 2 of the same letters for Enum.For example i want to make a spritning script that when i press W twice it starts to run.

game:GetService("UserInputService").InputBegan:connect(function(input,gameprocesed) if input.KeyCode == Enum.KeyCode.Z then for i = 1,16 do wait() game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed + 1.5 end end end)

game:GetService("UserInputService").InputEnded:connect(function(input,gameprocesed) if input.KeyCode == Enum.KeyCode.Z then for i = 1,16 do wait() game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed - 1.5 end end end)

game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Died:connect(function() game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 script.Disabled = true end)

1 answer

This is actually not that difficult to accomplish.

The primary function to use is tick(), which represents the time passed since epoch (June 1, 1970), in seconds. Why is this important? Well, the function is used to time certain things, such as a double jump or double click.

First create a variable that presents initial time:

local InitialTick = tick()

Then use the InputBegan event and check for a desired key:

local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.W and not gpe then

Set another variable for the current tick:

local CurrentTick = tick()

Now we check to see if the input received was indeed a double tap, press or click. This is accomplished by subtracting the InitialTick from the CurrentTick. If the time between key presses is either less than or exactly 1 second, then count it as a double tap, press or click.

Here's what that would look like:

local TimePassed = CurrentTick - InitialTick
if TimePassed <= 1 then
	-- Enable sprinting
	game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 50 -- Any desired number
end

Followed by the other ends:

	end
end)

If you also want the double press to be able to disable sprinting, you can use a boolean value and implement it into your code.

Example of doing this along with complete example script:

local uis = game:GetService("UserInputService")
local IsSprinting = false
local InitialTick = tick()
uis.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.W and not gpe then
		local CurrentTick = tick()
		local TimePassed = CurrentTick - InitialTick
		if TimePassed <= 1 and not IsSprinting then
			-- Enable sprinting
			IsSprinting = true
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 50 -- Any desired number
		elseif TimePassed <= 1 and IsSprinting then
			-- Disable sprinting
			IsSprinting = false
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 -- The original WalkSpeed of the Humanoid
		end
	end
end)

2 comments

0

What is gpe in your code? I'm not working on this right now but I was going to make a way for the player to choose what buttons he wanted for what abilities and I didn't know how to store a Key without HardCoding it into the path.

0

GPE stands for GameProcessedEvent. It is a boolean that determines whether or not the input was delivered without using any of the core client features, such as chat. If it is not associated with chatting, GPE is false.

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