I have 2 different scripts in my game, it is stored in individually in individual models. Both scripts are similar though. There are 6 models in my game, each having individual scripts. I don’t even know why the script activity is so high. I also don’t know how to optimize it, lags in-game.
Here’s the script,
local SSS = game:GetService("ServerScriptService")
local npcModule = require(SSS:WaitForChild("ModuleScripts"):WaitForChild("npcModule"))
local humanoidsTable = {}
local hitBox = script.Parent:WaitForChild("Hitbox")
local humanoid = script.Parent:WaitForChild("Humanoid")
local humanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent:WaitForChild("Head")
local weapon = script.Parent:WaitForChild("Weapon")
local attackAnim1 = hitBox:WaitForChild("AttackAnim1")
local attackAnim2 = hitBox:WaitForChild("AttackAnim2")
local attackAnim3 = hitBox:WaitForChild("AttackAnim3")
local attack1 = humanoid:WaitForChild("Animator"):LoadAnimation(attackAnim1)
local attack2 = humanoid:WaitForChild("Animator"):LoadAnimation(attackAnim2)
local attack3 = humanoid:WaitForChild("Animator"):LoadAnimation(attackAnim3)
local attackSound = humanoidRootPart.RootRigAttachment:WaitForChild("AttackSound")
local number = 1
local dead = false
local corou
local touchCon
humanoid.BreakJointsOnDeath = false
touchCon = hitBox.Touched:Connect(function(hitPart)
hitBox.CanTouch = false
if dead == true then
touchCon:Disconnect()
end
local enemyHumanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
if not table.find(humanoidsTable, enemyHumanoid) then
table.insert(humanoidsTable, enemyHumanoid)
if enemyHumanoid and enemyHumanoid.Health > 0 and not hitPart.Parent:FindFirstChild("Mob") then
enemyHumanoid:TakeDamage(4)
if number == 1 then
number = 2
attack1:Play()
elseif number == 2 then
number = 3
attack2:Play()
elseif number == 3 then
number = 1
attack3:Play()
end
task.wait(0.6)
attackSound:Play()
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
task.wait(0.65)
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50
task.wait(0.65)
end
table.remove(humanoidsTable, table.find(humanoidsTable, enemyHumanoid))
end
hitBox.CanTouch = true
end)
humanoid.Died:Once(function()
for index,joint in pairs(script.Parent:GetDescendants()) do
if joint:IsA("Motor6D") then
local socket = Instance.new("BallSocketConstraint")
local a1 = Instance.new("Attachment")
local a2 = Instance.new("Attachment")
a1.Parent = joint.Part0
a2.Parent = joint.Part1
socket.Parent = joint.Parent
socket.Attachment0 = a1
socket.Attachment1 = a2
a1.CFrame = joint.C0
a2.CFrame = joint.C1
socket.LimitsEnabled = true
socket.TwistLimitsEnabled = true
joint:Destroy()
end
end
head.CanCollide = false
game:GetService("Debris"):AddItem(hitBox, 0)
game:GetService("Debris"):AddItem(humanoidRootPart, 0)
game:GetService("Debris"):AddItem(weapon:FindFirstChildOfClass("WeldConstraint"), 0)
weapon.CanCollide = true
dead = true
task.wait(8)
game:GetService("Debris"):AddItem(script.Parent, 0)
end)
corou = coroutine.wrap(function()
while true do
if dead == true then
break
end
task.wait(1)
local target = npcModule.findNearestPlayer(10000, humanoidRootPart.Position)
if target ~= nil then
humanoid:MoveTo(target.Position)
else
humanoid:MoveTo(humanoidRootPart.Position + Vector3.new(math.random(-15, 15), 0, math.random(-15, 15)))
end
end
end)
corou()
Here’s another script, though it’s similar.
local SSS = game:GetService("ServerScriptService")
local npcModule = require(SSS:WaitForChild("ModuleScripts"):WaitForChild("npcModule"))
local humanoidsTable = {}
local hitBox = script.Parent:WaitForChild("Hitbox")
local humanoid = script.Parent:WaitForChild("Humanoid")
local humanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent:WaitForChild("Head")
local eye1 = script.Parent:WaitForChild("Eye1")
local eye2 = script.Parent:WaitForChild("Eye2")
local attackAnim1 = hitBox:WaitForChild("AttackAnim1")
local attackAnim2 = hitBox:WaitForChild("AttackAnim2")
local attackAnim3 = hitBox:WaitForChild("AttackAnim3")
local attack1 = humanoid:WaitForChild("Animator"):LoadAnimation(attackAnim1)
local attack2 = humanoid:WaitForChild("Animator"):LoadAnimation(attackAnim2)
local attack3 = humanoid:WaitForChild("Animator"):LoadAnimation(attackAnim3)
local attackSound = humanoidRootPart.RootRigAttachment:WaitForChild("AttackSound")
local number = 1
local dead = false
local corou
local touchCon
humanoid.BreakJointsOnDeath = true
touchCon = hitBox.Touched:Connect(function(hitPart)
hitBox.CanTouch = false
if dead == true then
touchCon:Disconnect()
end
local enemyHumanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
if not table.find(humanoidsTable, enemyHumanoid) then
table.insert(humanoidsTable, enemyHumanoid)
if enemyHumanoid and enemyHumanoid.Health > 0 and not hitPart.Parent:FindFirstChild("Mob") then
enemyHumanoid:TakeDamage(4)
if number == 1 then
number = 2
attack1:Play()
elseif number == 2 then
number = 3
attack2:Play()
elseif number == 3 then
number = 1
attack3:Play()
end
task.wait(0.6)
attackSound:Play()
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
task.wait(0.65)
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50
task.wait(0.65)
end
table.remove(humanoidsTable, table.find(humanoidsTable, enemyHumanoid))
end
hitBox.CanTouch = true
end)
humanoid.Died:Once(function()
eye1.Transparency = 1
eye2.Transparency = 1
game:GetService("Debris"):AddItem(hitBox, 0)
game:GetService("Debris"):AddItem(humanoidRootPart, 0)
dead = true
task.wait(8)
game:GetService("Debris"):AddItem(script.Parent, 0)
end)
corou = coroutine.wrap(function()
while true do
if dead == true then
break
end
task.wait(1)
local target = npcModule.findNearestPlayer(10000, humanoidRootPart.Position)
if target ~= nil then
humanoid:MoveTo(target.Position)
else
humanoid:MoveTo(humanoidRootPart.Position + Vector3.new(math.random(-15, 15), 0, math.random(-15, 15)))
end
end
end)
corou()
![]()
![]()