Here is a “Green Slime” NPC script, which doesnt lag at all
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local oldHealth = hum.Health
local damGUI = char:WaitForChild("Head"):WaitForChild("DamGUI")
local root = char:WaitForChild("HumanoidRootPart")
local attackRange = char:WaitForChild("AttackRange")
local damage = 10
local possibleTargets = game.ServerStorage:WaitForChild("ActivePlayers"):GetChildren()
if #possibleTargets > 1 then
target = possibleTargets[math.random(1,#possibleTargets)].Value:WaitForChild("HumanoidRootPart")
elseif #possibleTargets == 1 then
target = possibleTargets[1].Value:WaitForChild("HumanoidRootPart")
end
attackRange.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("PlayerVal") then
attackRange.CanTouch = false
hit.Parent:FindFirstChild("Humanoid").Health -= damage
root.AssemblyLinearVelocity = root.CFrame.LookVector * -200
task.wait(0.3)
attackRange.CanTouch = true
end
end)
hum.HealthChanged:Connect(function(newHealth)
local damTaken = oldHealth - newHealth
damGUI.StudsOffset = Vector3.new(math.random(0,1), math.random(0.5, 2), 0)
damGUI.TextLabel.Text = tostring(damTaken)
damGUI.Enabled = true
oldHealth = newHealth
task.wait(0.5)
damGUI.Enabled = false
end)
hum.Died:Connect(function()
task.wait(0.3)
local newChar1 = game.ServerStorage.NPCs.Other.GreenSlime1:Clone()
newChar1.Parent = char.Parent
newChar1:MoveTo(char.PrimaryPart.Position + Vector3.new(0,5,0))
local newChar2 = game.ServerStorage.NPCs.Other.GreenSlime1:Clone()
newChar2.Parent = char.Parent
newChar2:MoveTo(char.PrimaryPart.Position + Vector3.new(0,5,0))
game.ReplicatedStorage.EnemyCount.Value += 2
game.ReplicatedStorage.EnemyCount.Value -= 1
char:Destroy()
end)
while task.wait(0.05) do
hum:MoveTo(target.Position)
end
And here’s the BossSlime NPC script, which causes the enormous lag
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local oldHealth = hum.Health
local damGUI = char:WaitForChild("Head"):WaitForChild("DamGUI")
local root = char:WaitForChild("HumanoidRootPart")
local attackRange = char:WaitForChild("AttackRange")
local damage = 10
local possibleTargets = game.ServerStorage:WaitForChild("ActivePlayers"):GetChildren()
if #possibleTargets > 1 then
target = possibleTargets[math.random(1,#possibleTargets)].Value:WaitForChild("HumanoidRootPart")
elseif #possibleTargets == 1 then
target = possibleTargets[1].Value:WaitForChild("HumanoidRootPart")
end
attackRange.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("PlayerVal") then
attackRange.CanTouch = false
hit.Parent:FindFirstChild("Humanoid").Health -= damage
root.AssemblyLinearVelocity = root.CFrame.LookVector * -200
task.wait(0.3)
attackRange.CanTouch = true
end
end)
hum.HealthChanged:Connect(function(newHealth)
local damTaken = oldHealth - newHealth
damGUI.StudsOffset = Vector3.new(math.random(0,1), math.random(0.5, 2), 0)
damGUI.TextLabel.Text = tostring(damTaken)
damGUI.Enabled = true
oldHealth = newHealth
task.wait(0.5)
damGUI.Enabled = false
end)
hum.Died:Connect(function()
task.wait(0.3)
local newChar1 = game.ServerStorage.NPCs.Other.SlimeBoss1:Clone()
newChar1.Parent = char.Parent
newChar1:MoveTo(char.PrimaryPart.Position + Vector3.new(0,5,0))
local newChar2 = game.ServerStorage.NPCs.Other.SlimeBoss1:Clone()
newChar2.Parent = char.Parent
newChar2:MoveTo(char.PrimaryPart.Position + Vector3.new(0,5,0))
game.ReplicatedStorage.EnemyCount.Value += 2
game.ReplicatedStorage.EnemyCount.Value -= 1
char:Destroy()
end)
while task.wait(0.5) do
hum:MoveTo(target.Position)
end
As you can see, they are almost the same script.
Other differences are.
Green Slime - 100hp, green color
Boss Slime - 500hp, red color
I just found out that the size is not whats causing the issue, because I shrinked boss slime, and lag still persisted.
I cannot find any other differences, even the rigs are completely the same, the only physical difference is the color