Mobile only sprint buttons

Hi developers,

I have a localscript in StarterPlayerScripts and it works perfectly. The only thing i want it added is a button that only mobile players can see and when they hold it they sprint. How could i do this? This is my current code:

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local camera = workspace.Camera

local WalkSpeed = 16
local SprintSpeed = 25
local WalkFOV = 70
local SprintFOV = 90

local Tinfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local sprintgoal = {
	FieldOfView = SprintFOV
}

local walkgoal = {
	FieldOfView = WalkFOV
}

local sprinttween = TweenService:Create(camera, Tinfo, sprintgoal)
local walktween = TweenService:Create(camera, Tinfo, walkgoal)

UserInputService.InputBegan:Connect(function(input, gameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		sprinttween:Play()
		player.Character.Humanoid.WalkSpeed = SprintSpeed
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		walktween:Play()
		player.Character.Humanoid.WalkSpeed = WalkSpeed
	end
end)



local function endSprint(input, gameProcessed)

	if not gameProcessed then

		if input.UserInputType == Enum.UserInputType.Keyboard then

			local keycode = input.KeyCode

			if keycode == Enum.KeyCode.LeftShift then

				player.Character.Humanoid.WalkSpeed = walkSpeed

			end

		end

	end

end

Button example i want:
image

Thanks in advance for the help!

1 Like

The mobile buttons are from a service called ContextActionService. I don’t know how to use it but AlvinBlox made a video on it. Roblox Mobile Buttons Tutorial - Learn To Use ContextActionService - YouTube

I know how i could use this but I’m having a problem, the button won’t fit on all mobile devices.

Read this: Tips to Position and Scale UI - Community Tutorials - DevForum | Roblox

How can i use this with ContextActionService?

There was some code I found a while ago, im not sure where from / who it is by, but here it is:

local function getplatform()
	if (GuiService:IsTenFootInterface()) then
		return "Console"
	elseif (UserInputService.TouchEnabled and not UserInputService.MouseEnabled) then
		--touchscreen computers now have touchenabled so make sure to check for lack of mouse too
		--also, not all phones/tablets have accelerometer and/or gyroscope
		local DeviceSize = workspace.CurrentCamera.ViewportSize; 
		if ( DeviceSize.Y > 600 ) then
			return "Mobile (tablet)"
		else
			return "Mobile (phone)"
		end
	else
		return "Desktop"
	end
end

local Platform = getplatform()
print(Platform)

(in a local script)

If you want it to be regardless of what type of mobile it is, then this script would work:

local function getplatform()
	if (GuiService:IsTenFootInterface()) then
		return "Console"
	elseif (UserInputService.TouchEnabled and not UserInputService.MouseEnabled) then
		--touchscreen computers now have touchenabled so make sure to check for lack of mouse too
		--also, not all phones/tablets have accelerometer and/or gyroscope
			return "Mobile"
		end
	else
		return "Desktop"
	end
end

local Platform = getplatform()
if Platform == "Mobile" then
 --Button visibility and control script goes here
end

Nevermind, i found how to do it. Thanks for the help everybody.

1 Like

How’d you do it? It’s usually a good practice to tell how, as some people in the future might view this topic.