Run stopped when equipping/unequipping tool

Heya!

So, the problem is quite strange. When you equip tool in the first slot (number1) and try sprinting, your character will sprint no problem. However, when you equip tool in any other slot than 1 and try to sprint, you can’t move your character for a moment till received another input.

Still bug me out

Here the script

local sprintSpeed = 20

local sprintKey = Enum.KeyCode.LeftShift or Enum.KeyCode.RightShift

local USERINPUTSERVICE = game:GetService("UserInputService")

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local defaultSpeed = humanoid.WalkSpeed

local sprintAnim = script:WaitForChild("SprintAnim")
local animTrack = humanoid:LoadAnimation(sprintAnim)

local sprinting = false

local function nosprint()
	sprinting = false

	if animTrack.IsPlaying then
		animTrack:Stop()
	end

	humanoid.WalkSpeed = defaultSpeed
end

local function sprint()
	if not sprinting and humanoid.Health > 0 then
		sprinting = true

		defaultSpeed = humanoid.WalkSpeed

		if humanoid.MoveDirection.Magnitude > 0 then
			if not animTrack.IsPlaying then
				animTrack:Play()
			end
			humanoid.WalkSpeed = sprintSpeed
		end

		humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()

			if humanoid.MoveDirection.Magnitude <= 0 then
				nosprint()
			end
		end)
	end
end

local function onBegan(input, GPE)
	if GPE then
		return
	end

	if input.KeyCode == sprintKey then
		sprint()
	end
end

USERINPUTSERVICE.InputBegan:Connect(onBegan)

local function onEnded(input, GPE)
	if GPE then
		return
	end

	if input.KeyCode == sprintKey then
		nosprint()
	end
end

USERINPUTSERVICE.InputEnded:Connect(onEnded)

local function onDead()
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
end

humanoid.Died:Once(onDead)
1 Like