How to make this script mobile friendly?

local player = game.Players.LocalPlayer
local character = player.Character

local UserInputService = game:GetService("UserInputService")

local stamina = 200
local running = false

stamina = math.clamp(stamina,0,200)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		running = true
		character.Humanoid.WalkSpeed = 24
		while stamina > 0 and running do
			stamina = stamina - 1
			script.Parent:TweenSize(UDim2.new(stamina / 200,0,1,0),"Out","Linear",0)
			wait()
			if stamina == 0 then
				character.Humanoid.WalkSpeed = 18
			end
		end  
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		running = false
		character.Humanoid.WalkSpeed = 18
		while stamina < 200 and not running do 
			stamina = stamina + 1
			script.Parent:TweenSize(UDim2.new(stamina / 200,0,1,0),"Out","Linear",0)
			wait()
			if stamina == 0 then
				character.Humanoid.WalkSpeed = 18	
			end
		end
	end
end)

Use ContextActionService to bind this function to a button for mobile users

Here is a tutorial (by DevKing)

1 Like

like this?

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Touch then
		running = true
		character.Humanoid.WalkSpeed = 24
		while stamina > 0 and running do
			stamina = stamina - 1
			script.Parent:TweenSize(UDim2.new(stamina / 200,0,1,0),"Out","Linear",0)
			wait()
			if stamina == 0 then
				character.Humanoid.WalkSpeed = 18
			end
		end  
	end
end)

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Touch then
		running = false
		character.Humanoid.WalkSpeed = 18
		while stamina < 200 and not running do
			wait (5)
			stamina = 200
			local running = true
	
			
	        script.Parent:TweenSize(UDim2.new(stamina / 200,0,1,0),"Out","Linear",0)
			wait()
			if stamina == 0 then
				character.Humanoid.WalkSpeed = 18	
			end
		end
	end
end)
1 Like

No, you do not detect input using UserInputService, use ContextActionService instead. (refer to previous reply for tutorial and documentation)

the script above worked for me, thanks for your help