Sprint Button for Mobile/Tablet Compatibility

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    To make my game fully compatible with mobile and tablet.

  2. What is the issue? Include enough details if possible!
    In my game, sprinting requires Shift, so mobile/tablet players could not run.

  3. What solutions have you thought of so far?
    Adding a “Run” button for mobile/tablet users, but I have no idea how to script that.

I need help making my new Roblox game compatible with mobile/tablet users. It’s currently only playable on Computers. I want them to be able to play, but I have a Shift-key sprint script (which is not mine)… should I design a “Run” button or just leave it (and mobile/tablet users can’t sprint)? I am very new to scripting and don’t have any idea on how to script a run button for only mobile/tablet users… I don’t want to ask for a full script (that’s against the rules), but I do need a lot of help.

My game:
https://www.roblox.com/games/5644451718/Explore-And-Craft?refPageId=bbc5139d-0502-4db3-8252-f1779d77dd35

3 Likes

Remember to use the search feature!

Oh. Sorry. I’m moving this to Scripting Support and will try to figure out how to script one… That’s a really old post by the way.

Do some research on ContextActionService, it’s the best way to make it.

Ok, I will. I’m really new to scripting though.

I could be wrong about this as I have never scripted mobile support but i’d recommend looking into this function: UserInputService.TouchTap
You can use UserInputService.TouchEnabled to determine if the player is on mobile.

Using TouchEnabled to determine if a player is on mobile is bad practice as some devices have a keyboard and a touch screen. ContextActionService is a much better solution to this.

Is the run button a Hold to press then release or tap and tap again feature?

Okay, so I made a pretty basic sprint script that works on PC/Mobile.

On PC you have to hold down shift to sprint while on mobile you’d have to toggle it. Very mobile friendly.

PC:

Mobile:

local ContextActionService = game:GetService("ContextActionService")

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
character:WaitForChild("Humanoid")

local sprinting = false
local oldWS = character.Humanoid.WalkSpeed

local function sprint()

	sprinting = not sprinting
	if sprinting then
		character.Humanoid.WalkSpeed *= 1.8
	else
		character.Humanoid.WalkSpeed = oldWS
	end
	
end

ContextActionService:BindAction("Sprint", function(action, state, input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		sprint()
	elseif input.UserInputType == Enum.UserInputType.Touch and state == Enum.UserInputState.Begin then
		sprint()
	end
end, true, Enum.KeyCode.LeftShift)
6 Likes

Currently, there is no run button. You hold the shift key to sprint.

Thanks! I have a few questions:

  • Is the script inside a GUI button? Could I use a TextButton that said “Sprint” instead?
  • Is it a Script or LocalScript?
  • Would I need anything else such as other scripts, values, etc?

No, you have to bind an image to it with the SetImage function. You can also use the SetPosition function to change where it is on the screen.

It takes 2 parameters, the actionName and the imageAssetId. The SetPosition takes in a UDim2, instead of an asset id.

For example,

local ContextActionService = game:GetService("ContextActionService")

... -- after the bind action code
ContextActionService:SetImage("Sprint", "rbxassetid://0") -- replace 0 with the asset id
ContextActionService:SetPosition("Sprint", UDim2.new(0,0,0,0)) -- fiddle around with the position to get a good result.

Reading through the ContextActionService documentation should help you a lot. As well as a few tutorials on YouTube if you get stuck.

2 Likes

I was thinking about making a TextButton… is there any way that would work? If not, I’ll try to make an image that looks like a TextButton…

1 Like

Perhaps using the SetTitle function? You’ll have to do the rest in your own.