How do i detect if a player stopped clicking on a mobile button?

ok, so i am working on a mobile sprint button, but then i realized:

this thing won’t work with stamina unless it detects when the player releases there finger off it
but i have no idea how to do that so… any help?

here’s the script i’m using:

-- followed a tutorial and modified the code a bit since
-- i don't know how to use mobile input systems [ yet ]

local cas = game:GetService("ContextActionService")
local run = Enum.KeyCode.LeftShift
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("runre")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local rootpart = char:WaitForChild("HumanoidRootPart")
local Stamina = script.Parent:WaitForChild("Stamina", 1)
local Running = false
local PlayAnim

StaminaMath = math.clamp(Stamina.Value, 0, 100)
local function handleSprint()
if Stamina.Value <= 0 then return end
	char.Humanoid.WalkSpeed = 24
	local Anim = Instance.new('Animation')
	Anim.AnimationId = 'rbxassetid://7384132764'
	PlayAnim = char.Humanoid:LoadAnimation(Anim)
	PlayAnim.Looped = true
	PlayAnim:Play()
	Running = true
	while Stamina.Value > 0 and Running do
		wait(0.12)
		Stamina.Value = Stamina.Value - 1
		script.Parent:TweenSize(UDim2.new(Stamina.Value/ 100, 0, 1, 0), "Out", "Linear",0)
		if Stamina.Value == 0 then
			char.Humanoid.WalkSpeed = 16
			Running = false
			PlayAnim:Stop()
			PlayAnim.Looped = false
		end
	end
end

cas:BindAction("Sprint", handleSprint, true)
cas:SetPosition("Sprint", UDim2.new(.2,0,.5,0))
cas:SetTitle("Sprint", "Sprint")
cas:GetButton("Sprint").Size = UDim2.new(.3,0,.4,0)

game:GetService("RunService").RenderStepped:Connect(function()
	script.Parent.Parent:WaitForChild("StaminaStatus").Text = math.floor(Stamina.Value).."/"..("100")
end)

that’s all,
thank you for reading!

Use MouseButton1Down and MouseButton1Up, should work just fine…

1 Like

You could use the UserInputType “touch” and the UserInputState “End” or “Cancel”, these arguments are passed to your function in the following order: actionName, inputState, inputObject

The inputObject is used to find what UserInputType your input is, and the inputState is self-explanatory, actionName is self-explanatory.

1 Like