I have a unit which cycles through all the children of workspace that fulfill the if conditions states after the for loop. Then, if those conditions are met, it will fire a ray and attach a beam to deal damage.
The problem is that the ray is often fired more than once in succession at all the units in its range. I've tried using a break after the damage is dealt but that makes it so that damage is dealt in rapid succession to only one unit.
My question is whether my method of using a for i,v in pairs loop to search for a unit is good or whether there is another way to find a single unit.
I've attached the function itself below.
function attackShoot()
for i,v in pairs(workspace:GetChildren()) do -- thisUnit is the unit trying to shoot.
if thisUnit.Configuration.Team.Value ~= 0 and -- not neutral
thisUnit.Configuration.Team.Value ~= -1 and -- not dead
v:FindFirstChild("Configuration") and -- is a unit
v.Configuration.Team.Value ~= 0 and -- not neutral
v.Configuration.Team.Value ~= -1 and -- not dead
v.Configuration.Team.Value ~= thisUnit.Configuration.Team.Value and -- not in the same team
v.Configuration.UnitType.Value == "infantrySupport" and -- infantry cannot fight vehicles
(v.Torso.Position - thisUnit.Torso.Position).Magnitude <= range -- checks if in range
then
local ray = Ray.new(thisUnit.Torso.Position, (v.Torso.Position - thisUnit.Torso.Position).Unit * range)
local hit, pos = workspace:FindPartOnRay(ray, nil, false, true)
if hit.Parent:FindFirstChild("Humanoid", false) then -- checks if enemy is in LOS
inLOS = true
elseif not hit.Parent:FindFirstChild("Humanoid", false) then
inLOS = false
break
end
if hit.Name == "ray" then
wait(math.random(1.9, 3.9))
end
if inLOS == true then
thisUnit.Torso.Beam.Attachment1 = v.Torso.Attachment
end
wait(0.1)
thisUnit.Torso.Beam.Attachment1 = nil
if hit and hit.Parent and hit.Parent.Humanoid then
hit.Parent.Humanoid:TakeDamage(math.random(1.9, 3.9))
end
break
end
end
end
end
while true do
wait(math.random(1.9, 3.9))
inLOS = true -- resets inLOS
attackShoot()
end

1 comment
Thank you! I've been meaning to look around OOP and ModuleScripts (and also matrices, but that's math) but I keep procrastinating.