Mobile Sprint Button

Hello, I am trying to find some tutorials that show how to create a mobile sprint button, not even Youtube has those kind of videos. There were none so, I am asking here if anyone knows how I can script this.

5 Likes

Just create a Remote Event inside of ReplicatedStorage called “SprintEvent” or whatever you’d like, and then you could code something like

-- Client Side
local RepStorage = game:GetService("ReplicatedStorage")
local Event = RepStorage:WaitForChild("SprintEvent")
local Button = -- wherever the button is

local Sprinting = false
Button.MouseButton1Click:Connect(function()
	Sprinting = not Sprinting
	Event:FireServer(Sprinting)
end)


-- Server Side
local RepStorage = game:GetService("ReplicatedStorage")
local Event = RepStorage:WaitForChild("SprintEvent")

local DefaultSpeed = 16
local SprintSpeed  = -- whatever speed you'd like

Event.OnServerEvent:Connect(function(Player, Sprinting)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")
	Humanoid.WalkSpeed = (Sprinting and SprintSpeed) or DefaultSpeed
end)

Ahh nice, thank you. Where do I put that script you posted?

Using ContextActionService you can create mobile buttons, but they’re not the most versatile.

local ContextActionService = game:GetService"ContextActionService"
local LocalPlayer = game:GetService"Players".LocalPlayer
ContextActionService:BindAction("Sprint",function(name,state,input)
    if state == Enum.UserInputState.Begin then
        local char = LocalPlayer.Character
        if char then
            char.Humanoid.WalkSpeed = 30
        end
    else
        local char = LocalPlayer.Character
        if char then
            char.Humanoid.WalkSpeed = 16
        end
    end
    return Enum.ContextActionResult.Pass
end,true,Enum.KeyCode.LeftShift,Enum.KeyCode.RightShift)
ContextActionService:SetPosition("Sprint",UDim2.new(0,0,0.5,0))
ContextActionService:SetTitle("Sprint","Sprint")
ContextActionService:GetButton("Sprint").Size = UDim2.new(0.5,0,0.5,0)
18 Likes

Thank you so much. This is really helpfull.

1 Like

So the code under “Client Side” should go in a local script somewhere, and the code under “Server Side” should go in a normal script, probably under ServerScriptService.

2 Likes

Why have a remote for the sprint? It seems like it would only add a fair bit of latency.

1 Like

idk I thought it would be good practice, but they can obviously just do it on the client if they want

1 Like

Oh my gosh it worked, thank you so much. It means alot to me.

2 Likes