You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
To make my game fully compatible with mobile and tablet.
What is the issue? Include enough details if possible!
In my game, sprinting requires Shift, so mobile/tablet players could not run.
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.
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.
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)
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.