Sprint for Mobile and Console

What I am trying to do is make a sprint that is compatible for Mobile and Console and PC
The PC Sprint has been scripted. I would like to know how would I add on to this script for console and mobile.
How I would Like the mobile to sprint is for when the player moves the move button to 100%
For Xbox kind like Minecraft sprinting were just push the left joystick forward twice, as fast as you can

local sprintSpeed = 30 
local normalSpeed  =16
local function sprintCharacter(on) 
	if plr.Character then
		if on then 
			plr.Character.Humanoid.WalkSpeed = sprintSpeed
		else 
			plr.Character.Humanoid.WalkSpeed = normalSpeed
		end
	end
end

game:GetService("UserInputService").InputBegan:connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then 
			sprintCharacter(true) 
		end
	end
end)
game:GetService("UserInputService").InputEnded:connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then 
			sprintCharacter(false) 
		end
	end
end)
1 Like

I would recommend looking into ContextActionService

It provides the ability to connect multiple keybinds or controller presses towards a single function. They can also be disconnected easily. For mobile devices you can set it to automatically create and remove touch buttons.

1 Like

I suggest you use ContextActionService as @CodeJared said, and also I noticed that on doesn’t = to anything really.

local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")

local sprintSpeed = 30 
local normalSpeed  =16

local Character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()

local Humanoid = Character:FindFirstChildOfClass("Humanoid")


local function Sprint(actionName, inputState, inputObject)
	if actionName == "Sprint" and inputState == Enum.UserInputState.Begin then
		Humanoid.WalkSpeed = sprintSpeed
	elseif actionName == "Sprint" and inputState == Enum.UserInputState.End then
		
		Humanoid.WalkSpeed = normalSpeed
	end
end

ContextActionService:BindAction(“Sprint”,Sprint,true,Enum.KeyCode.LeftShift,Enum.KeyCode.ButtonY)

And for Editing mobile buttons there an article about it

4 Likes