How to tell how long a mobile user has pressed the jump button

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Title of the post, :How to tell how long a mobile user has pressed the jump button
  2. What is the issue? Include screenshots / videos if possible!
    No idea how, and no other posts to help
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    UIS.JumpRequest helps, but it doesn’t say how long u held
UIS.JumpRequest:Connect(function()

end)
3 Likes

Someone please, oh please answer this

1 Like

You could set a variable to time.os() when the player first presses down on the jump button, and then another variable set to time.os() after the player releases the button. After that, subtract the first time.os() from the second and then you have how long the player held the jump button in seconds.

There’s a path to the Roblox mobile touch UI, but it only shows up when the player is using the Touch InputType. So, you need to put the pathing code to the mobile buttons in a Pcall.


Edit: I’d personally not make functionality in my game input-dependent. (That meaning that something only works for Touch and not keyboards or gamepads).

So, maybe the Landed and Jumping stuff from the State machine in humanoids would help. But that could be messy or not work like I think. I’m on my phone right now so trying to explain it can be difficult.

1 Like

give me an example, I have no idea how to do this. You didn’t even specify how I sjould do it

Video:


File:

KnowHowLongPlayerHoldsDownMobileJumpButton.rbxl (53.2 KB)


Code & Explanation (code comments):

The following local script is located under StarterPlayer → StarterCharacterScripts:

local UserInputService = game:GetService("UserInputService")
local pathFound = false
local JumpButton_Path

local startTime
local endTime

local inputBeganConnection
local inputEndedConnection

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Touch then
		-- The above means the player is using Touch-based input/controls. 
		-- Roblox only has the TouchGui in the Explorer when the player is using touch controls. 
		-- If the inputType is changed, it seems to delete the TouchGui.
		
		if pathFound == false then
			local success, errorMessage = pcall(function()
				JumpButton_Path = script.Parent.Parent.PlayerGui:WaitForChild("TouchGui").TouchControlFrame:WaitForChild("JumpButton")
			end)
			if success then -- if Roblox found the path, then:
				pathFound = true -- setting this to true stops the path from being contantly updated when the player moves around and whatnot

				--print(JumpButton_Path:GetFullName()) -- this is to check the path, just in case of errors or the script location is changed.

				inputBeganConnection = JumpButton_Path.InputBegan:Connect(function()
					startTime = os.time()
				end)

				inputEndedConnection = JumpButton_Path.InputEnded:Connect(function()
					endTime = os.time()
					--print(endTime, startTime)
					local timeHeld = endTime - startTime
					print("Player held jump button for "..timeHeld.." seconds")
				end)

			end
		end

	else
		pathFound = false -- resets the ability to check for the TouchGui -> Jump Button incase the player changes inputTypes to something else and then back to Touch-based input
	end
end)

Relevant Roblox Docs:

3 Likes

Whoah, you’re a lifesaver, I would have never found out all that.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.