Problem with script

basically on mobile, when you stop holding the sprint button it continues to make you sprint whenever you change directions, here’s a video of what happens

Video

ServerScript
local players = game:GetService("Players")

local character = script.Parent
local player = players:GetPlayerFromCharacter(character)

local remoteEvent = script:WaitForChild("Sprint")

local sprinting = false
local minumumStamina = 5

function sprint(plr, value)
	if value == false then
		plr.Character.Humanoid.WalkSpeed = 16
		sprinting = false
	end
	if plr.Values.Stamina.Value > minumumStamina and value then
		plr.Character.Humanoid.WalkSpeed = 30
	end
end

remoteEvent.OnServerEvent:Connect(function(plr, value)
	if plr == player then
		local staminaValue = plr.Values.Stamina
		
		if value == false then
			sprinting = false
			sprint(plr, value)
		end
		
		if staminaValue.Value < minumumStamina or plr.Character.Humanoid.Health <= 0 then
			return
		end
		if value == true then
			if sprinting then return end
			sprinting = true
			sprint(plr, value)
		end
		while sprinting == true and staminaValue.Value > 0 do
			task.wait()
			staminaValue.Value -=1
			if staminaValue.Value <= 0 or not sprinting or plr.Character.Humanoid.Health <= 0 then
				remoteEvent:FireClient(plr)
				break
			end
		end
	end
end)
LocalScript

(i doubt this is the problem)

local UIS = game:GetService("UserInputService")

local tweenService = game:GetService("TweenService")
local players = game:GetService("Players")

local remoteEvent = script.Parent:WaitForChild("Sprint")

local camera = workspace.CurrentCamera

local character = script.Parent.Parent
local player = players.LocalPlayer

local canSprint = false

local originalFOV = 70
local sprintFOV = 90

local staminaValue = player.Values.Stamina

local minumumStamina = 5

local playerGui = player.PlayerGui
local roundGui = playerGui:WaitForChild("RoundGui")
local sprintButton = roundGui:WaitForChild("MobileButtons"):WaitForChild("SprintButton")

local isHolding = false

if not UIS.TouchEnabled then
	return
end

function tweenFOV(fov)
	if not isHolding then return end
	local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
	local tween = tweenService:Create(camera, tweenInfo, {FieldOfView = fov})
	tween:Play()
end

sprintButton.InputBegan:Connect(function()
	isHolding = true
	
	if staminaValue.Value > minumumStamina and character.Humanoid.Health > 0 and isHolding then
		tweenFOV(sprintFOV)
		remoteEvent:FireServer(true)
	end
end)

sprintButton.InputEnded:Connect(function()
	isHolding = false
	
	tweenFOV(originalFOV)
	remoteEvent:FireServer(false)
end)

remoteEvent.OnClientEvent:Connect(function()
	tweenFOV(originalFOV)
end)
1 Like

hey! try swapping InputBegan/InputEnded for MouseButton1Down/MouseButton1Up on the button instead. touch events can be unreliable on mobile and sometimes just don’t fire when you lift your finger, so the server never gets the ‘stop sprinting’ signal and you get stuck sprinting. MouseButton1Down/MouseButton1Up a lot more consistently for both touch and mouse.

3 Likes