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

Detecting if a player click twice in a row?

Im really confused i wanna make the character double jump when the player clicks a specified key 2 times but i can't seem to find a way to do that!

2 answers

✓ Accepted answer

Put in a local script and put the local script in StarterPlayerScripts.

local UserInputService = game:GetService("UserInputService")
local localPlayer = game.Players.LocalPlayer
local character
local humanoid 

local canDoubleJump = false
local hasDoubleJumped = false
local oldPower
local TIME_BETWEEN_JUMPS = 1
local DOUBLE_JUMP_POWER_MULTIPLIER = 2

function onJumpRequest()
    if not character or not humanoid or not character:IsDescendantOf(workspace) or
     humanoid:GetState() == Enum.HumanoidStateType.Dead then
        return
    end

    if canDoubleJump and not hasDoubleJumped then
        hasDoubleJumped = true
        humanoid.JumpPower = oldPower * DOUBLE_JUMP_POWER_MULTIPLIER
        humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
    end
end

local function characterAdded(newCharacter)
    character = newCharacter
    humanoid = newCharacter:WaitForChild("Humanoid")
    hasDoubleJumped = false
    canDoubleJump = false
    oldPower = humanoid.JumpPower

    humanoid.StateChanged:connect(function(old, new)
        if new == Enum.HumanoidStateType.Landed then
            canDoubleJump = false
            hasDoubleJumped = false
            humanoid.JumpPower = oldPower
        elseif new == Enum.HumanoidStateType.Freefall then
            wait(TIME_BETWEEN_JUMPS)
            canDoubleJump = true
        end
    end)
end

if localPlayer.Character then
    characterAdded(localPlayer.Character)
end

localPlayer.CharacterAdded:connect(characterAdded)
UserInputService.JumpRequest:connect(onJumpRequest)

2 comments

0

This is a plausible effort, but this really shouldn't be recommended to him. The NumberValue is unnecessary, and the while loop pulse will not be in sync with input timestamps.

0

I tried this with my friends and did a test session of 5 mins. It worked in sync and didn't lag. This option is plausible and works. I was using my knowledge to help and this is the way I found. Your it's more complicated to understand.

Simple. All you'd have to do is record the timestamp of the initial input, and set an interval for how long the program has before the same input no longer counts as a "double click". Here's an example:

local interval = 0.5 -- How long before the second input event expires
local timestamp = tick() -- Current time stamp of input

if tick() - timestamp <= interval then
	... user double clicked
else -- if they missed the second input event
	timestamp = tick() -- double click resets
end

All you'd have to do is integrate this within actual ROBLOX events, that correspond to the input you're listening for. Here's a more practical example:

-- Yes, we're going to use UserInputService.
local InputService = game:GetService("UserInputService")

-- Click timing
local interval = 0.5
local timestamp = tick()

-- Key that will activate the double press function
local doubleClickKey = Enum.KeyCode.A

-- Function for when double press occurs
local function doublePress()
	print("Key was pressed twice within interval")
end

InputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode == doubleClickKey and not processed then
		-- Same logic
		if tick() - timestamo <= interval then
			doublePress()
		else
			timestamp = tick()
		end
	end
end)

Hope this helps, let me know if you have any questions.

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