Please Help With Making A Mobile Sprinting System!

I need to modify my script to make it compatible with mobile but i don’t know how to?

I tried to make a sprint button then checked when it was clicked on mobile but it didn’t work

i’m using RunService.RenderStepped for the sprinting on PC how would i modify it to also work on mobile? here is the script below responsible for sprinting

– SERVICES –
local Players = game:GetService(“Players”)
local UIS = game:GetService(“UserInputService”)
local TweenService = game:GetService(“TweenService”)
local RunService = game:GetService(“RunService”)

– VARIABLES –
local camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild(“Humanoid”) :: Humanoid

local gui = player.PlayerGui:WaitForChild(“StaminaGui”)

local boolean = false

local currentStamina = 100
local origFov = camera.FieldOfView

– CONSTANTS –
local REGULAR_SPEED = humanoid.WalkSpeed
local SPRINT_SPEED = REGULAR_SPEED + 8
local MAX_STAMINA = 100
local STAMINA_REDUCE = 0.3
local STAMINA_INCREASE = 0.4
local SPRINT_FOV = 85
local SPRINT_COOLDOWN = 3

local shiftDown = false
local sprintingCooldown = false
local canReplenishStamina = true

– FUNCTIONS –
local function updateStaminaBar()
if boolean == true then return end
if not gui.Enabled then
gui.Enabled = true
local tween1 = TweenService:Create(gui.Border.Outline, TweenInfo.new(0.3), {[“Transparency”] = 0})
local tween2 = TweenService:Create(gui.Border.Fill, TweenInfo.new(0.3), {[“BackgroundTransparency”] = 0})
tween1:Play()
tween2:Play()
end
gui.Border.Fill.Size = UDim2.new(1,0,currentStamina/MAX_STAMINA,0)
if gui.Border.Fill.Size.Y.Scale >= 1 and gui.Border.Outline.Transparency <= 0 then
local tween1 = TweenService:Create(gui.Border.Outline, TweenInfo.new(0.3), {[“Transparency”] = 1})
local tween2 = TweenService:Create(gui.Border.Fill, TweenInfo.new(0.3), {[“BackgroundTransparency”] = 1})
gui.Enabled = true
tween1:Play()
tween2:Play()
tween2.Completed:Wait()
gui.Enabled = false
end
end

– HANDLERS –
UIS.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end

if input.KeyCode == Enum.KeyCode.LeftShift then
	if currentStamina <= 0 then return end
	shiftDown = true
end

end)

UIS.InputEnded:Connect(function(input, gameProcessed)
if gameProcessed then return end

if input.KeyCode == Enum.KeyCode.LeftShift then
	shiftDown = false
end

end)

RunService.Heartbeat:Connect(function()
if shiftDown and currentStamina > 0 and humanoid.MoveDirection.Magnitude > 0 then
if player.Team == “Dead” then
boolean = true
elseif player.Team == “Alive” then
boolean = false
end
– ensure shift is held down, make sure we have enough stamina, and we’re moving
if camera.FieldOfView ~= SPRINT_FOV then
local cameraTween = TweenService:Create(camera, TweenInfo.new(0.8), {[“FieldOfView”] = SPRINT_FOV})
cameraTween:Play()
end
humanoid.WalkSpeed = SPRINT_SPEED
currentStamina -= STAMINA_REDUCE
else
– either shift is not held down, or we’re not moving, or we don’t have enough stamina
if camera.FieldOfView ~= origFov then
local cameraTween = TweenService:Create(camera, TweenInfo.new(0.8), {[“FieldOfView”] = origFov})
cameraTween:Play()
end
humanoid.WalkSpeed = REGULAR_SPEED
if currentStamina <= 0 then
currentStamina = 0 – make sure to set stamina to 0 incase it became negative
– we have no stamina left, so force a cooldown before regenerating stamina
if sprintingCooldown then return end
sprintingCooldown = true
canReplenishStamina = false
shiftDown = false
task.wait(SPRINT_COOLDOWN)
canReplenishStamina = true – let us replenish stamina BEFORE we disable the sprinting cooldown, otherwise,
– stamina will still be at 0, which could cause the sprintingCooldown to be enabled again and never allow us to replenish stamina
end
end
if currentStamina < MAX_STAMINA then
if not canReplenishStamina then return end
– don’t update the stamina if we’re in a cooldown
if not shiftDown or humanoid.MoveDirection.Magnitude <= 0 then
currentStamina += STAMINA_INCREASE
sprintingCooldown = false
end
updateStaminaBar()
end
end)

1 Like