Hello developers so im trying to make some lasers and miniguns that shoot an enemy target that gets within its range. I would imagine i gave the system a good cooldown if it couldnt find a target but even when having a minigun and laser script (that are basically the exact same script shown below) decrease the performance of the game drastically. Is it because there are too many enemys spawning in or is it something about the script. Either way any help is appreciated!
local lasersenabled = false
function ToggleLasers(enemy)
if enemy == nil and lasersenabled == false then return end
for i, tier in pairs(script.Parent:GetChildren()) do
if tier:IsA("BasePart") and tonumber(string.sub(tier.Name, 5, 5)) <= game.Workspace.ItemValues.Lasers.Tier.Value then
if enemy == nil then
lasersenabled = false
tier.Right.Beam.Attachment1 = nil
tier.Left.Beam.Attachment1 = nil
else
lasersenabled = true
tier.Right.Beam.Attachment1 = enemy.HumanoidRootPart.Attachment
tier.Left.Beam.Attachment1 = enemy.HumanoidRootPart.Attachment
end
end
end
end
function Attack()
local target = attackModule.LocateTarget(script.Parent.Parent.Parent, range)
if game.Workspace.GameValues.RaidOngoing.Value == false then ToggleLasers(nil) return end
if script.Parent.Parent.Parent.Stunned.Value == true then ToggleLasers(nil) task.wait(1) return Attack() end
if target then else ToggleLasers(nil) task.wait(0.5) return Attack() end
if target and target:FindFirstChild("HumanoidRootPart") and target.Humanoid and target.Humanoid.Health > 0 then
game.ReplicatedStorage.Events.LookToTarget:Fire(target.HumanoidRootPart.Position)
end
if target and target.Humanoid and target.Humanoid.Health > 0 then
attackModule.DamageTarget(target, damage)
script.Parent.Parent.Parent.Fire:Play()
ToggleLasers(target)
end
task.wait(cooldown)
return Attack()
end
game.Workspace.GameValues.RaidOngoing.Changed:Connect(function(val)
if val == true then Attack() end
end)