Hey, i have question, how to handle script where a lot of units will attack target, and not lag it??? any tips on optimization of it??? is there any trick to do that?
Hey! I noticed your question about optimizing a script for units attacking a target without lag. To handle this, focus on batch processing units together each frame and checking their proximity using distance calculations. Use the Heartbeat event to sync updates with the frame rate for smoother performance. If you have any questions or need more help, feel free to ask
--example
local target = game.Workspace.Target
local units = {}
for i = 1, 100 do
local unit = Instance.new("Model")
unit.Name = "Unit" .. i
table.insert(units, unit)
end
local function updateUnits()
for _, unit in pairs(units) do
local unitTarget = target.Position
local unitPosition = unit.PrimaryPart.Position
if (unitPosition - unitTarget).Magnitude < 10 then
print(unit.Name .. " attacking target")
end
end
end
game:GetService("RunService").Heartbeat:Connect(function()
updateUnits()
end)
Is this AI? If so, please don’t do this, many of the people on these forums need help and AI does not know how to handle code
No , it’s not AI, i only wan’t to know how to optimize units like in the conquerors 3 that attack one target in cooldown, I mean something like this:
Unit get enemy unit in range: start shooting > enemy is out of range : stop shooting
i made a script for detecting part in bounds and i think it’s not optimized:
local Model = script.Parent
local Enemies = Model.Enemies
local Player = Model.Player
local Collection_Service = game:GetService("CollectionService")
local radius = 10
local function Tag(ins:Instance)
if ins and ins.Parent == Enemies and ins:IsA("BasePart") and not ins:HasTag("Enemy") then
Collection_Service:AddTag(ins,"Enemy")
elseif ins:HasTag("Enemy") then
ins:RemoveTag("Enemy")
end
end
Enemies.ChildAdded:Connect(Tag)
Enemies.ChildRemoved:Connect(Tag)
local array = {}
local function SetPart(parts:{BasePart})
for i = 1, #parts, 1 do
local part = parts[i]
table.insert(array,part)
part.Material = "Neon"
part.BrickColor = BrickColor.new("Lime green")
end
for i = 1, #array do
local part = array[i]
if part and (part.Position-Player.Position).Magnitude > radius then
part.Material = "Plastic"
part.BrickColor = BrickColor.new("Really red")
array[i] = nil
end
end
end
while true do
local params = OverlapParams.new()
params.FilterDescendantsInstances = Collection_Service:GetTagged("Enemy")
params.FilterType = Enum.RaycastFilterType.Include
local Parts = workspace:GetPartBoundsInRadius(Player.Position,radius,params) or nil
SetPart(Parts)
task.wait(1/8)
end
i know this code is not organized, and i made it in 20 minutes or soo
Oh, I see how it might seem that way Just to clear things up, I came up with the script myself, but I did get some help from AI when explaining the optimization techniques detailed I’m really not good at those.
one question more, is 1/5 of second good for detecting things in distance??? for example it’s not smooth, but 1/8 is good for me, i know the more you wait, you more optimizing, i don’t need run service, this would be crazy xd
Faster updates after 1/5 of a second may use more CPU For optimization, 1/8 second is suitable. Checking various times and CPU usage aids in optimizing for the best outcome
soo 1/8 of second is the best ???