Hello There!
I’m creating a 2D Tower-Defense game where you summon units to destroy the enemy base and defeating enemies along the way. The issue is, as the title says, whenever I spawn my units and they kill a couple of enemies, the framerate drops because when I move my mouse, it starts to get all laggy and delay-ly?
This is the script that summons the friendly units:
local SUMMON_UNITS = game:GetService("ReplicatedStorage"):WaitForChild("SUMMON_UNIT")
local CollectionService = game:GetService("CollectionService")
SUMMON_UNITS.OnServerEvent:Connect(function(Plr,UnitWant)
local unitExist = game.ReplicatedStorage:FindFirstChild(UnitWant)
if unitExist then
local Unit = unitExist:Clone()
Unit.Parent = workspace.CurrentFriendlies
CollectionService:AddTag(Unit, "FriendlyTag")
for _, Bodypart in pairs(Unit:GetDescendants()) do
if Bodypart:IsA("BasePart") then
Bodypart.CollisionGroup = "NPCs"
end
end
while task.wait() do
if Unit:GetAttribute("IsAttacking") == false then
Unit:WaitForChild("Humanoid"):Move(Vector3.new(-1, 0, 0))
else
Unit:WaitForChild("Humanoid"):Move(Vector3.new(0, 0, 0))
end
end
else
warn("Unit "..unitExist.." doesn't exist.")
end
end)
And this is the script to control the friendly battler. (Btw, the friendly battler also has an enemy version of itself.)
local RadicalNPC = script.Parent
local Humanoid = RadicalNPC:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
Humanoid.BreakJointsOnDeath = false
--//Service
local Debris = game:GetService("Debris")
local CollectionService = game:GetService("CollectionService")
--//AI
local CD = false
game:GetService("RunService").Stepped:Connect(function()
if not CD then
if RadicalNPC:HasTag("EnemyTag") then
for _, Enemy in pairs(workspace.CurrentFriendlies:GetChildren()) do
if Enemy:IsA("Model") and Enemy:HasTag("FriendlyTag") then
local Hum = Enemy:FindFirstChildWhichIsA("Humanoid")
local Magnitude = (Enemy.PrimaryPart.Position - RadicalNPC.HumanoidRootPart.Position).Magnitude
if Magnitude <= 3 and CD ~= true and Hum.Health >= 1 then
CD = true
RadicalNPC:SetAttribute("IsAttacking", true)
local AttackAnim = Animator:LoadAnimation(script.ATKAnim)
AttackAnim:Play()
Hum:TakeDamage(10)
AttackAnim.Ended:Wait()
task.wait(2)
RadicalNPC:SetAttribute("IsAttacking", false)
CD = false
end
end
end
elseif RadicalNPC:HasTag("FriendlyTag") then
for _, Enemy in pairs(workspace.CurrentEnemies:GetChildren()) do
if Enemy:IsA("Model") and Enemy:HasTag("EnemyTag") then
local Hum = Enemy:FindFirstChildWhichIsA("Humanoid")
local Magnitude = (Enemy.PrimaryPart.Position - RadicalNPC.HumanoidRootPart.Position).Magnitude
if Magnitude <= 3 and CD ~= true and Hum.Health >= 1 then
CD = true
RadicalNPC:SetAttribute("IsAttacking", true)
local AttackAnim = Animator:LoadAnimation(script.ATKAnim)
AttackAnim:Play()
Hum:TakeDamage(50)
AttackAnim.Ended:Wait()
task.wait(2)
RadicalNPC:SetAttribute("IsAttacking", false)
CD = false
end
end
end
end
else
return
end
end)
--//Death
local DeathConnection
DeathConnection = Humanoid.Died:Connect(function()
for _, M6D in pairs(RadicalNPC:GetDescendants()) do
if M6D:IsA("Motor6D") then
local ballSocket = Instance.new("BallSocketConstraint")
local Attach0, Attach1 = Instance.new("Attachment"), Instance.new("Attachment")
Attach0.Parent = M6D.Part0
Attach1.Parent = M6D.Part1
ballSocket.Parent = M6D.Parent
ballSocket.Attachment0 = Attach0
ballSocket.Attachment1 = Attach1
Attach0.CFrame = M6D.C0
Attach1.CFrame = M6D.C1
ballSocket.TwistLimitsEnabled = true
ballSocket.LimitsEnabled = true
M6D:Destroy()
end
end
RadicalNPC.Head.CanCollide = true
RadicalNPC["Left Arm"].CanCollide = true
RadicalNPC["Right Arm"].CanCollide = true
local DeathSound = Instance.new("Sound",RadicalNPC.HumanoidRootPart)
DeathSound.SoundId = "rbxassetid://6763801684"
DeathSound:Play()
DeathConnection:Disconnect()
task.wait(3)
Debris:AddItem(RadicalNPC,0)
end)