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

[Solved] JumpRequest triggering too many times?

Hello,

I am working on a script that makes you fly when you double press jump. When a player on the computer double jumps, it works perfectly. However, when a mobile player double jumps with their special jump button, they only need to jump once to start flying (but twice to stop flying??). I think the reason this does not work is because JumpRequest gets triggered multiple times during a jump but I am not sure how to fix this.

Here is the part for mobile players that I am struggling with:

function JumpRequest()
	if uis.TouchEnabled and not uis.KeyboardEnabled and not uis.MouseEnabled then
		if not Tapped then
            Tapped = true
            wait(TapTime)
			Tapped = false
		else
			if not flying then
				fly()
			else
				endFlying()
			end
		end
	end
end

uis.JumpRequest:Connect(JumpRequest)

And this is how I do it for computer players:

uis.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		if not Tapped then
            Tapped = true
            wait(TapTime)
			Tapped = false
		else
			if not flying then
				fly()
			else
				endFlying()
			end
		end
	end
end)

By the way, if there is a way to merge both of these together, I am open to suggestions!

Thanks for your help and time!

2 answers

First, you can easily reduce it so 21 lines instead of 32, and also, just add debounce.

More information: https://developer.roblox.com/en-us/api-reference/event/UserInputService/JumpRequest

https://developer.roblox.com/en-us/articles/Debounce

local userInputService = game:GetService("UserInputService")
local debounce = false
function jumpRequest()
    if debounce == false then
        debounce = true
        if not Tapped then
            Tapped = true
            wait(TapTime)
            Tapped = false
        else
            if not flying then
                fly()
            else
                endFlying()
            end
        end
        wait(3)
        debounce = false
    end
end

userInputService.JumpRequest:Connect(JumpRequest)

Solved It!

local debounce = false
function JumpRequest()
	if not debounce then
		debounce = true
		wait(0.01)
		debounce = false
		if not Tapped then
			Tapped = true
			wait(TapTime)
			Tapped = false
		else
	        if not flying then
	            fly()
	        else
	            endFlying()
	        end
		end
	end
end

uis.JumpRequest:Connect(JumpRequest)

Thank you for your help!

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