Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Detect double jump?

Asked by 9 years ago

This is not a request. I am simply asking you guys to point me in the right direction cause i would like to know how to detect if spacebar has been pressed twice quickly for a double jump. I dont need the double jump its self cause I know how to script that. Im asking how would my script detect if a person double tapped spacebar?

0
Why not use the existing script at the part they jump a second time..? alphawolvess 1784 — 9y
0
I dont know what you mean sorry ^_^ Alucifer 45 — 9y

2 answers

Log in to vote
0
Answered by
DrJonJ 110
9 years ago

Here's some generic code that will work:

local t = 0;
local player = game.Players.LocalPlayer;
local mouse = player:GetMouse();
local pressed = false;

mouse.KeyDown:connect(function(key)
    if string.byte(key) == 32 then --32 is the space bar
        if not pressed then
            pressed = true
            repeat
                wait()
                t = t +0.1
            until t >= 0.75 or pressed == false
            if pressed == false and t < 0.75 then
                t = 0
                print('Double jump!')-- double jump
            elseif t >= 0.75 then
                t = 0
                pressed = false         
            end
        elseif pressed then
            pressed = false
        end
    end
end)
1
Please explain your code, and don't just give out code. woodengop 1134 — 9y
0
It works thanks, you've just taught me something new :) Alucifer 45 — 9y
Ad
Log in to vote
1
Answered by
drahsid5 250 Moderation Voter
9 years ago

I know this is already answered but the solution up there is terrible, in my opinion.

It's best to check if the player is grounded before letting them jump again.

repeat wait() until game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name) --WaitForChild could be better.
--Defining variables--
plr = game.Players.LocalPlayer.Character["Left Leg"] --Define the left leg
p = game.Players.LocalPlayer --Define the player
ks = {} --Table for keystrokes
ip = game:GetService("UserInputService") --Define user input service
jump1 = false --Debounce
jump2 = false --Debounce

function isGround(hit) --Define function for checking if hit is the floor
    if string.lower(hit.Name) == "baseplate" or string.lower(hit.Name) == "ground" or string.lower(hit.Name) == "floor" or string.lower(hit.Name) == "floor" then --This line could be skipped, I just find it better to tag what is ground, so you can't jump off of non cancollide parts
        return true --Return that it's on the floor
    else return false --Not?
    end
end

function plrGrounded() --Define the function for creating the ray to find the ground
    local ray = Ray.new(plr.Position, Vector3.new(0, -10, 0)) --Create the ray
    local hit, position = game.Workspace:FindPartOnRay(ray, plr.Parent) --Define hit and position, ignoring the player
    if  (position - plr.Position).magnitude <= 1.5  and isGround(hit) then return true else return false  end --If the player is really close to the ground and the function isGround returns true then return true, if not then return false
end

inp = coroutine.wrap(function() --Define coroutine for input detection
game:GetService("RunService").RenderStepped:connect(function()  --Look for input every frame
    ip.InputBegan:connect(function(k,p) --Input start
            ks[k.KeyCode] = true --This key is pressed
        end)

    ip.InputEnded:connect(function(k,p) --Input end
            ks[k.KeyCode] = false --The key isn't pressed
        end)    
    end)
end)

sj = coroutine.wrap(function() --Second jump coroutine
    game:GetService("RunService").RenderStepped:connect(function() --Run once per frame
        if jump1 == true and jump2 == false then --if the first jump is true and the player hasn't jumped twice
            wait(0.1) --small pause to prevent a 'launch'   
            if ks[Enum.KeyCode.Space] == true then --If space has been pressed
            jump2 = true --Second jump is true
            plr.Velocity = plr.Velocity + Vector3.new(0,25,0) --Apply the second jump
            end
        end
    end)
end)

--Run--
inp()
sj()

p.Character.Humanoid.Jumping:connect(function() --First jump function
        jump1 = true --First jump is true
        repeat wait() until plrGrounded() == true --Wait until the player is grounded
        jump1 = false --Jump 1 isn't true
        jump2 = false --Jump 2 isn't true

end)

Answer this question