How to add button for mobile player

My goal is to make sprinting by shifting in every devices ( except xbox and something else )

This is my completed script for making sprinting & stamina system :

local cam = game.Workspace.CurrentCamera
local players = game.Players.LocalPlayer
local humanoid = players.Character:FindFirstChildOfClass("Humanoid")

local userinputservice = game:GetService("UserInputService")
local tweenservice = game:GetService("TweenService")
local replicatedstorage = game:GetService("ReplicatedStorage")
local contextactionservice = game:GetService("ContextActionService")
local runservice = game:GetService("RunService")

local statscript = replicatedstorage:FindFirstChild("StatScript")
local stat = require(statscript)

local isrunning_fov = tweenservice:Create(cam, stat.fovinfo, {FieldOfView = stat.normalfov + 20})
local isnotrunning_fov = tweenservice:Create(cam, stat.fovinfo, {FieldOfView = stat.normalfov})

local isrunning_gui = tweenservice:Create(script.Parent, stat.staminafadinginfo, {BackgroundTransparency = 0})
local isnotrunning_gui = tweenservice:Create(script.Parent, stat.staminafadinginfo, {BackgroundTransparency = 1})

local isrunning_guistroke = tweenservice:Create(script.Parent.UIStroke, stat.staminafadinginfo, {Transparency = .8})
local isnotrunning_guistroke = tweenservice:Create(script.Parent.UIStroke, stat.staminafadinginfo, {Transparency = 1})

local isrunning = nil
local allow_running = true
local max = 0.968
local sizechangingamount = 20/100 * max
local cd_running = 50/100 * max

local function walkspeed (num)
	humanoid.WalkSpeed = num
end

local function sprint (active)
	if active then
		isrunning = true
		isrunning_fov:Play()
		isrunning_gui:Play()
		isrunning_guistroke:Play()
		walkspeed(stat.normalspeed + 4)		
	else
		isrunning = false
		isnotrunning_fov:Play()
		isnotrunning_gui:Play()
		isnotrunning_guistroke:Play()
		walkspeed(stat.normalspeed)
	end
end

task.wait()

script.Parent.BackgroundTransparency = 1
script.Parent.UIStroke.Transparency = 1


runservice.Heartbeat:Connect(function (deltaTime)
	if isrunning == true then
		
		if script.Parent.Size.X.Scale > 0 then
			script.Parent.Size -= UDim2.fromScale(sizechangingamount * deltaTime,0)
		else
			allow_running = false
		end
		
	elseif isrunning == false then
		
		if script.Parent.Size.X.Scale < max then
			script.Parent.Size += UDim2.fromScale(sizechangingamount * deltaTime,0)
		else
			
		end
		
	end
	
	if script.Parent.Size.X.Scale > cd_running then
		allow_running = true
	end
	
end)


userinputservice.InputBegan:Connect(function (input)
	if input.KeyCode == Enum.KeyCode.LeftShift and humanoid.MoveDirection.Magnitude > 0 then
		if allow_running == true then
			
			sprint(true)

			repeat task.wait() until not userinputservice:IsKeyDown(Enum.KeyCode.LeftShift) or allow_running == false
			
		end
		
		sprint(false)
		
	end
end)

I want to know how to make a button for those devices like mobile and tablet. This looks simple for some people, but I want to know how to make it with understandable explains.

you can use an ImageButton or a TextButton (your choice) and check if the player is on mobile using UIS (if im not wrong) and inside that button add a localscript where you will check if it has been pressed, then fire the shiftlock function

1 Like

Thank you so much. I’ll try that.

1 Like

I forgot to mention other things: you can use if UIS.TouchEnabled then to see if player is on a mobile device. If they are on mobile, then you would set the GUI button’s Visible to true. If they are on other devices, set it to false. This will be inside a LocalScript. You can use RemoteEvents or something to use shiftlock. As for the GUI buttons you can use Button.MouseButton1Click:Connect(function() to detect player input.

P.S. UIS is UserInputService.

1 Like

Oh sorry I was doing a stamina system. Let me edit real quick.

Since you’re already using ContextActionService you could just use BindAction to setup the keys/buttons.
BindAction gives you the option of automatically adding a button on TouchDevices for that particular action.
You can find out more here:

1 Like