Hello, I have a button for mobile players that allow them to sprint. However, when I test on a mobile gadget, in real life, when I hold the sprint button sometimes my sprint arbitrarily regenerates in a random stamina value even though I’m holding the button down, I know I’m holding it down because when I let go I can see the button darken, as the button brightens when I’m holding it. When I test this in studio with a mobile emulation, like a iPhone, this problem does not occur. This is my script:
local ContextActionService = game:GetService"ContextActionService"
local player = game.Players.LocalPlayer
local tweenService = game:GetService("TweenService")
local stamina = 90.7
local canRun = false
local running = false
local canSprint = true
local canRegen = true
math.clamp(stamina, 0, 100)
local tweenInfo = TweenInfo.new(
1,
Enum.EasingStyle.Linear
)
function buttonPressed(name, state, input)
local char = player.Character
local bar = player.PlayerGui:WaitForChild("sprintGui").background.Frame
if state == Enum.UserInputState.Begin and canSprint == true and game.ReplicatedStorage.values.round.Value == true and not char.HumanoidRootPart:FindFirstChild("iAmBen") then
game.Workspace.soundEffects.breathingSound:Stop()
char.Humanoid.WalkSpeed = 24
canRun = true
running = true
while stamina >= 0 and running == true do
stamina -= .1
if stamina <= 75 and stamina > 50 then
canSprint = true
stamina -= .05
local Goal = {BackgroundColor3 = Color3.fromRGB(144, 156, 36)}
tweenService:Create(bar, tweenInfo, Goal):Play()
end
if stamina <= 50 and stamina > 25 then
canSprint = false
stamina -= .1
local Goal = {BackgroundColor3 = Color3.fromRGB(161, 99, 33)}
tweenService:Create(bar, tweenInfo, Goal):Play()
end
if stamina <= 25 then
canSprint = false
stamina -= .15
local Goal = {BackgroundColor3 = Color3.fromRGB(168, 0, 24)}
tweenService:Create(bar, tweenInfo, Goal):Play()
end
bar:TweenSize(UDim2.new(stamina / 100, 0, 0.7 ,0), "Out", "Linear", 0)
task.wait()
if stamina <= 0 then
char.Humanoid.WalkSpeed = 16
end
end
else
if game.ReplicatedStorage.values.round.Value == true and not char.HumanoidRootPart:FindFirstChild("iAmBen") then
running = false
char.Humanoid.WalkSpeed = 16
if stamina <= 25 then
game.Workspace.soundEffects.breathingSound:Play()
end
if canRegen == true then
while stamina < 90.7 and not running do
stamina += .05
canRegen = false
if stamina >= 75 then
canSprint = true
game.Workspace.soundEffects.breathingSound:Stop()
local Goal = {BackgroundColor3 = Color3.fromRGB(0, 160, 0)}
tweenService:Create(bar, tweenInfo, Goal):Play()
end
if stamina <= 75 and stamina > 50 then
canSprint = true
game.Workspace.soundEffects.breathingSound:Stop()
local Goal = {BackgroundColor3 = Color3.fromRGB(144, 156, 36)}
tweenService:Create(bar, tweenInfo, Goal):Play()
end
if stamina <= 50 and stamina > 25 then
game.Workspace.soundEffects.breathingSound:Stop()
canSprint = false
local Goal = {BackgroundColor3 = Color3.fromRGB(161, 99, 33)}
tweenService:Create(bar, tweenInfo, Goal):Play()
end
if stamina <= 25 then
canSprint = false
local Goal = {BackgroundColor3 = Color3.fromRGB(168, 0, 24)}
tweenService:Create(bar, tweenInfo, Goal):Play()
end
bar:TweenSize(UDim2.new(stamina / 100, 0, 0.7 ,0), "Out", "Linear", 0)
task.wait()
if stamina == 0 then
char.Humanoid.WalkSpeed = 16
end
end
end
canRegen = true
end
end
return Enum.ContextActionResult.Pass
end
game.ReplicatedStorage.values.round.Changed:Connect(function()
local char = player.Character
local sprintButton = ContextActionService:BindAction("sprintButton", buttonPressed, true)
ContextActionService:SetPosition("sprintButton",UDim2.new(0,0,500,0))
ContextActionService:SetTitle("sprintButton","Sprint")
if game.ReplicatedStorage.values.round.Value == true and not char.HumanoidRootPart:FindFirstChild("iAmBen") then
task.wait(9.5)
local char = player.Character
if not char.HumanoidRootPart:FindFirstChild("iAmBen") then
ContextActionService:SetPosition("sprintButton",UDim2.new(0.72,-20,0.20,-20))
ContextActionService:SetTitle("sprintButton","Sprint")
end
end
if game.ReplicatedStorage.values.round.Value == false then
ContextActionService:UnbindAction("sprintButton")
ContextActionService:SetPosition("sprintButton",UDim2.new(0,0,500,0))
ContextActionService:SetTitle("sprintButton","Sprint")
end
end)
I have no clue what the issue is and how to solve it, I tried putting prints for each stamina segment (by segment I mean the if statements comparing the stamina value), but then I found out that the regeneration was arbitrary, it regenerates in different stamina values, it doesn’t matter what segment its in.