Mobile Sprinting Script

There was a similar post to this, but it didn’t have any replies.

I am making a mobile and PC sprinting script, the pc sprinting one is fine, but the Mobile one works when running, but it doesnt know how to stop when i click the mobile button again.

If anyone can simplify or help fix this script, it would be much appreciated. I can also provide a video or more of the script if needed for better understanding.

---- > My Problem: It does not end when clicked button again, but it starts when I click the button.

Here’s the local script:

local function onSprintInput(input, isProcessed)
	if isProcessed then return end
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if input.UserInputState == Enum.UserInputState.Begin then
			sprintHeld = true
			if humanoid.MoveDirection.Magnitude > 0 and not exhausted then
				sprint(true)
			end
		elseif input.UserInputState == Enum.UserInputState.End then
			sprintHeld = false
			sprint(false)
		end
	end
end
local function onSprintInput2(input, isProcessed) ---function for mobile side
	if isProcessed then return end
	if input.UserInputState == Enum.UserInputState.Begin then
		sprinting = false
		sprintHeld = true
		if humanoid.MoveDirection.Magnitude > 0 and not exhausted then
			sprint(true)
			sprinting = true
			if exhausted then
				sprint(false)
				sprinting = false
			end
		end
	elseif input.UserInputState == Enum.UserInputState.End then
		sprinting = false

		sprint(false)
		
	end
end

-- Update stamina UI
local function updateStaminaUI()
	playerGui.StaminaGUI.StaminaFrame.StaminaBar.Size = UDim2.new(math.clamp(stamina / maxStamina, 0, 1), 0, 1, 0)
end

-- updates stamina and time
local function updateSprintState(deltaTime)
	if sprinting then
		if humanoid.MoveDirection.Magnitude > 0 then
			stamina = math.max(0, stamina - drainRate * deltaTime)
			if stamina == 0 then
				sprint(false)
				exhausted = true
				task.delay(recoveryDelay, function() exhausted = false end)
			end
		else
			sprint(false)
		end
	elseif not exhausted then
		stamina = math.min(maxStamina, stamina + refreshRate * deltaTime)
		if sprintHeld and humanoid.MoveDirection.Magnitude > 0 then
			sprint(true)
		end
	end
	if not sprinting then
		sprinting = false
		sprint(false)
	end
	updateStaminaUI()
end

-- Handle landing state change
local function onStateChanged(_, newState)
	if newState == Enum.HumanoidStateType.Landed and sprintHeld and humanoid.MoveDirection.Magnitude > 0 and not exhausted then
		manageSprintAnimation(true)
	end
end



-- Connections
userInputService.InputBegan:Connect(onSprintInput) --- pc side
userInputService.InputEnded:Connect(onSprintInput) --- pc side
runService.Heartbeat:Connect(updateSprintState)
humanoid.StateChanged:Connect(onStateChanged)
sprintButton.InputBegan:Connect(onSprintInput2) ---mobile button
sprintButton.InputEnded:Connect(onSprintInput2) ---mobile button

-- Ensure character and humanoid are properly loaded
if not character or not character.Parent then
	player.CharacterAdded:Wait():WaitForChild("Humanoid")
end
	elseif input.UserInputState == Enum.UserInputState.End then
		sprinting = false

		sprint(false)
		
	end

Would it be that you meant to put sprintHeld = false instead of sprinting = false here?

1 Like

Ah! That helps. Now it works, i just had to do this, THANK YOU!

elseif input.UserInputState == Enum.UserInputState.End then
		sprinting = false
                sprintheld = false --- i added this :)
		sprint(false)
		
	end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.