Recently I’ve been developing a horror game, and a couple days ago I was making mobile support for a sprint system that already works perfectly fine on PC (the code block listed below is the PC version)
It even works on consoles, but it won’t work on my mobile buttons. I’ve tried making different sprinting systems dedicated just for mobile, I’ve tried modifying this one, but it won’t work.
The goal for this is the mobile player taps and holds the sprint button, and they sprint, when they let go, they stop.
The following screenshots is the layout of my Mobile Buttons, and how they look in game.
The following code is my sprint system that previously works for PC.
local PlayerStamina_ = {
BaseWalkSpeed = 10,
SprintSpeed = 16,
Stamina = 100,
MaxStamina = 100,
CrouchSpeed = 4,
RechargeTime = 2,
}
RunService.RenderStepped:Connect(function(delta)
local Stamina = Humanoid:GetAttribute("Stamina")
if Stamina < StaminaUsage then
IsSprinting = false
Humanoid.WalkSpeed = Humanoid:GetAttribute("BaseWalkSpeed")
TweenService:Create(LowStamina, TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {ImageTransparency = 0.2}):Play()
TweenService:Create(VeinStamina, TweenInfo.new(0.5, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {ImageTransparency = 0.1}):Play()
task.wait(2)
TweenService:Create(LowStamina, TweenInfo.new(5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {ImageTransparency = 1}):Play()
TweenService:Create(VeinStamina, TweenInfo.new(5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {ImageTransparency = 1}):Play()
end
if IsSprinting and Stamina > 0 and Humanoid.MoveDirection.Magnitude > 0 then
Humanoid:SetAttribute("Stamina", Stamina - StaminaUsage)
LastSprint = tick()
else
if tick() - LastSprint >= RechargeTime and Stamina < Humanoid:GetAttribute("MaxStamina") then
Humanoid:SetAttribute("Stamina", Stamina + StaminaUsage)
end
end
end)
UserInputService.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
IsSprinting = true
Humanoid.WalkSpeed = PlayerStamina_.SprintSpeed
end
end)
UserInputService.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
IsSprinting = false
Humanoid.WalkSpeed = PlayerStamina_.BaseWalkSpeed
end
end)
Humanoid:GetAttributeChangedSignal("Stamina"):Connect(function()
local Stamina = Humanoid:GetAttribute("Stamina")
StaminaBar.Fill.Fill.Size = UDim2.fromScale(Stamina / 100, 1)
end)
*Note that the system initially ran on ContextActionService but was modified to use UserInputService
How can I fix this? Are there any other better ways to make this work? Thank you for your time.