My "shift to sprint" toggle mode only works when the character is running

My shift to sprint has 2 different modes, toggle and hold. Hold makes it so you have to hold the button to sprint, while toggle will toggle the shift to sprint when you press the button, but will only stop if the player is running

Video of what’s going on

Things i have tried

  • changing userinputstate to something else
    *changing some stuff around in lines 41-56

This is the script

wait(0.1)
local Player = game.Players.LocalPlayer

local Character = Player.Character

local Humanoid = Character.Humanoid

local RunAnimation = Instance.new("Animation")

RunAnimation.AnimationId = "rbxassetid://7006481757" -- You must create your own animation or it wont work

RAnimation = Humanoid:LoadAnimation(RunAnimation)


Running = false


function Handler(BindName, InputState)
	if game.Players.LocalPlayer.PlayerGui.Settings.ToggleSprint.Value == false then
		if InputState == Enum.UserInputState.Begin and BindName == 'RunBind' then

			Running = true

			Humanoid.WalkSpeed = 17 -- The run speed

		elseif InputState == Enum.UserInputState.End and BindName == 'RunBind' then

			Running = false

			if RAnimation.IsPlaying then

				RAnimation:Stop()

			end

			Humanoid.WalkSpeed = 16    -- The walk speed

		end
	else
		if InputState == Enum.UserInputState.Begin and BindName == 'RunBind' then
			if Humanoid.WalkSpeed == 16 then
			Running = true

			Humanoid.WalkSpeed = 20 -- The run speed

			else

			Running = false

			if RAnimation.IsPlaying then

				RAnimation:Stop()

			end

				Humanoid.WalkSpeed = 16 
			end
		end
	end
end


Humanoid.Running:Connect(function(Speed)

	if Speed >= 10 and Running and not RAnimation.IsPlaying then

		RAnimation:Play()

		Humanoid.WalkSpeed = 20

	elseif Speed >= 10 and not Running and RAnimation.IsPlaying then

		RAnimation:Stop()

		Humanoid.WalkSpeed = 16

	elseif Speed < 10 and RAnimation.IsPlaying then

		RAnimation:Stop()

		Humanoid.WalkSpeed = 16    

	end

end)


Humanoid.Changed:Connect(function()

	if Humanoid.Jump and RAnimation.IsPlaying then

		RAnimation:Stop()

	end

end)


game:GetService('ContextActionService'):BindAction('RunBind', Handler, true, Enum.KeyCode.LeftShift) -- Enum.KeyCode."LeftShift" is the button you will use to run
workspace.Animator.Humanoid.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.None then
		
	end
end)
game:GetService("ContextActionService"):SetImage("RunBind", "rbxassetid://7015072120")
game:GetService("ContextActionService"):SetPosition("RunBind", UDim2.new(0, 75,0, 57))

sorry for my god awful scripting, i’m still learning.

so in short, sprint only toggles off when player is running.

Why dont you use UserInputService? I’ll sure that it’ll work

it’s because i want it to be compatible with other devices easily

it might be a problem on the humanoid.Running part since you can only change while running

the if statement that is in a running line will start when the player run so it will check you when you running only