Hello, I am making a sprint button for mobile and XBOX. The idea is that when you HOLD the sprint button on mobile, or hold the thumbstick or hold L Shift then you sprint until you either run out of energy or you release the button.

The problem is that I have made the code and it doesn't do anything and I am not sure why. There are no errors because the script works but not properly.

local contextActionService = game:GetService("ContextActionService")
local UIS = game:GetService('UserInputService')

local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

local player = game:GetService'Players'.LocalPlayer

local NormalWalkSpeed = 16
local NewWalkSpeed = 32

local power = 10

local sprinting = false

repeat wait() until game.Players.LocalPlayer.Character

local character = player.Character

function onButtonPress(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin  then
		character.Humanoid.WalkSpeed = NewWalkSpeed
		sprinting = true
		while power > 0 and sprinting do
			power = power - .03
			Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
			--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
	else
		local character = player.Character or player.CharacterAdded:Wait()
		character.Humanoid.WalkSpeed = NormalWalkSpeed
						sprinting = false
						while power < 10 and not sprinting do
							power = power + .03
							Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
							--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)
							wait()
							if power <= 0 then
						character.Humanoid.WalkSpeed = NormalWalkSpeed
						end
				end
			end
		end
	end
end

local mobilebutton = contextActionService:BindAction("SprintButton",onButtonPress,true,Enum.KeyCode.LeftShift, Enum.KeyCode.Thumbstick1)
contextActionService:SetPosition("SprintButton",UDim2.new(0.72,-25,0.20,-100))
contextActionService:SetImage("SprintButton", "http://www.roblox.com/asset/?id=5632374236")