Game freezes with this function

Hello, i use this function for my gun run system. But for some reason if you press shift your game freezes for 0.5s
Any ideas why?

local animation1
local animation2
local function GunRun(PlayOrStop)
	if Character:FindFirstChildWhichIsA("Tool") then
		local v = Character:FindFirstChildWhichIsA("Tool")
			if v:FindFirstChild("Animations") and not animation2 then
				animation2 = Humanoid:LoadAnimation(v:FindFirstChild("Animations").Run) 
			end
			if PlayOrStop == "Play" then
			
				if v:FindFirstChild("Animations") then
					if v:FindFirstChild("Animations").Run and animation2 and not animation2.IsPlaying then
						animation2:Play()
					end
				end
				v.Unequipped:Connect(function()
					if v:FindFirstChild("Animations") then
						if v:FindFirstChild("Animations").Run and animation2 then
							animation2:Stop()
							animation2 = nil
						end
					end
				end)
			elseif PlayOrStop == "Stop"  then
				
				if v:FindFirstChild("Animations") then
					if v:FindFirstChild("Animations").Run and animation2 then
						animation2:Stop()
						animation2 = nil
				end
			end
		end
	end
end

1 Like

when you press shift, do you execute anything? any script or some function

1 Like

Yes, a sprint event. It didnt lag before i added this so.

1 Like

show the part that executes the function

1 Like
local function StartRun()
	if ValuesFolder.Sprinting.Value == false then
		game.ReplicatedStorage.CharStats:FindFirstChild(Character.Name).Sprinting.Value = true
		GunRun("Play")
		ValuesFolder.Sprinting.Value = true
		script.Parent.CharacterClient.RemoteEvent:FireClient(game.Players:GetPlayerFromCharacter(Character), "FOV", 80, 1)
	end
end