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.
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)