My games lags when I enable my NPC script

I coded a npc that is inspired from the anime High Rise invasion, the main script (attacks, detect torso, damage, etc…) is making this lag.

I can’t find why please help.

When I enable the script my fps go crazy
I coded a npc that is inspired from the anime High Rise invasion, the main script (attacks, detect torso, damage, etc…) is making this lag.

I can’t find why please help.

When I enable the script my fps go crazy :
image


The script :


local temp = nil

local hitresult

local Debris = game:GetService("Debris")

local RunService = game:GetService("RunService")

local FIRST

local debounce = false

local TweenService = game:GetService("TweenService")




function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end
local attack

function at()
	attack = math.random(1,4)
	print("y")
end

local dmg

local cd = 5
local canrad = true
local cc = true
script.Parent.Attacks.Hitbox.Touched:Connect(function(hit)
	if cc == true then
		dmg = 50

		local torsohit = findNearestTorso(script.Parent.Torso.Position)

		if torsohit ~= nil then
			cc = false
			local to = torsohit
			
			local l = script.Parent.Mask

			



			if attack == 1 and not debounce then
				debounce = true
				canrad = false
				script.Parent.Humanoid.WalkSpeed = 0

				local AttackFolder = Instance.new("Folder")
				AttackFolder.Parent = workspace
				AttackFolder.Name = "AttackFolder"

				FIRST = Instance.new("Part")
				FIRST.Material = Enum.Material.Neon
				FIRST.CanCollide = false
				FIRST.Anchored = true
				FIRST.Color = Color3.new(255, 0, 0)
				FIRST.Size = Vector3.new(12.217, 12.283, 12.217)
				FIRST.Orientation = Vector3.new(0,0,0)
				FIRST.Transparency = 0
				FIRST.CanTouch = true
				FIRST.Parent = AttackFolder

				local Mesh = Instance.new("SpecialMesh")
				Mesh.Parent = FIRST
				Mesh.MeshType = Enum.MeshType.Sphere


				local effect = script.Effect:Clone()
				effect.Transparency = 0
				effect.CanTouch = true
				effect.Parent = AttackFolder

				FIRST.CFrame = torsohit.CFrame + Vector3.new(0,50,0)
				effect.CFrame = torsohit.CFrame + Vector3.new(0,50,0)
				Debris:AddItem(AttackFolder, 10)
				Debris:AddItem(FIRST, 10)
				Debris:AddItem(Mesh, 10)
				Debris:AddItem(effect, 10)

				local Animation = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.Attacks.One.Animation)




				wait(2)
				local tweenInfo = TweenInfo.new(
					0.5,
					Enum.EasingStyle.Circular,
					Enum.EasingDirection.Out,
					0,
					false,
					0
				)

				local tween1 = TweenService:Create(FIRST, tweenInfo, {Position = torsohit.Position})
				local tween2 = TweenService:Create(effect, tweenInfo, {Position = torsohit.Position})

				Animation:Play()
				tween1:Play()
				tween2:Play()
				torsohit.Parent.Humanoid.Health = torsohit.Parent.Humanoid.Health - dmg
				print(torsohit.Parent.Humanoid)
				wait(2

				)
				local Info = TweenInfo.new(
					0.2-- the speed of the tween
					,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0-- tween style
				)

				local Goals = {Size=Vector3.new(50,50,50)}-- the size to tween to
				local Goals1 = {Size=Vector3.new(60,9,60)}-- the size to tween to
				local tween = game:GetService("TweenService"):Create(FIRST,Info,Goals)
				tween:Play()
				local tweens = game:GetService("TweenService"):Create(effect,Info,Goals1)
				tweens:Play()


				local min = 1
				local max = 200

				while wait() do

					min = min + 1

					effect.Position = FIRST.Position

					if min >= max then
						break;
					end


				end

			

				script.Parent.Humanoid.WalkSpeed = 16
				debounce = false
				canrad =true
			elseif attack == 2 and not debounce then
				debounce = true
				canrad = false
				local humanoid = torsohit.Parent.Humanoid
				script.Parent.Humanoid.WalkSpeed = 16

				script.Parent.Humanoid:MoveTo(torsohit.Position)

				humanoid.WalkSpeed = 0

				wait(2)

				script.Parent.Humanoid.WalkSpeed = 0
				local Animation = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.Attacks.Two.Animation)
				Animation:Play()


				Animation:GetMarkerReachedSignal("Hit"):Connect(function()
					humanoid.Health = humanoid.Health - 10

				end)

				script.Parent.Humanoid.WalkSpeed = 16
				humanoid.WalkSpeed = 16
				debounce = false
				canrad = true
			elseif attack == 3 and not debounce then
				debounce = true
				local AttackFolder = Instance.new("Folder")
				AttackFolder.Parent = workspace
				AttackFolder.Name = "AttackFolder"
				Debris:AddItem(AttackFolder, 4)
				local humanoid = torsohit.Parent.Humanoid
				local NHum = script.Parent.Humanoid

				humanoid.WalkSpeed = 0
				NHum.WalkSpeed = 0


				local Beam = script.Beam:Clone()
				Beam.Transparency = 0
				Beam.CanTouch = true
				Beam.Parent = AttackFolder

				Debris:AddItem(Beam, 4)

				local Start = script.Start:Clone()
				Start.Transparency = 0
				Start.CanTouch = true
				Start.Parent = AttackFolder

				Debris:AddItem(Start, 4)


				script.Parent.HumanoidRootPart.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position, torsohit.Position)

				wait(2)


				Beam.CFrame = script.Parent.Torso.CFrame*CFrame.new(0,0,-15)
				Start.CFrame = script.Parent.Torso.CFrame*CFrame.new(0,0,-7)

				wait(1)

				debounce = false
				humanoid.WalkSpeed = 16
				NHum.WalkSpeed = 16
				canrad = true
			end
		end
		
		wait(2)
		
		cc = true
	end
	


end)



In Studio (without the morph)

Thanks.

I think your script is causing memory spikes. You can solve memory issues by disconnecting functions you don’t need. Handling Events. Also I see that you have a lot of variables set to nil which takes up memory as well.

1 Like