Hello, everyone I’m just starting to script, and I’m fairly sure the solution here is simple, but I can’t seem to find a solution of my own. Do tell me if the code structure is not great, I will try to improve it; as I’ve said I don’t know anything better, I just searched on the internet and picked up what works.
Goal: The ship (on the right) should fire at the existing nearest ship, but once the nearest ship is destroyed, the ship will not fire onto the nearest existing ship unless if the second ship moves closer than the first ship’s position
Issue: Ship (on the right) not firing at the second existing ship (the further one), unless second existing ship is closer than the first ship that was destroyed The issue should be around the line where assigning the NearestShip to v.
while true do
-- Workspace.Ships:Childrens
for i, v in pairs(OverallShipsFolder:GetChildren()) do
-- Workspace.Ships:Childrens:Childrens
for i, v in pairs(v:GetChildren()) do
-- Checks if nearby ship doesn't belong to owner
if v.Values["Ship.Owner"].Value ~= Ship.Parent.Name then
local DistanceFromNearestShip = (Ship.Position - v.Position).Magnitude
if DistanceFromNearestShip <= 200 and v.Values.Stats["Ship.Integrity"].Value > 0 then
local EnemyShipSpeed = v.Velocity.Magnitude
-- If there is no nearest ship and there is a nearby ship, assign
if not NearestShip then
NearestShip = v
-- Else if nearestship is closer to the ship then, assign
else
if DistanceFromNearestShip < (NearestShip.Position - Ship.Position).Magnitude then
NearestShip = v
end
end
-- Fire at enemy ship
ShipState.Value = "In Combat"
if Statistics["Attack.BulletCount"].Value ~= 0 then
local Recoil = Vector3.new(NearestShip.Position.X + math.random(-Statistics["Attack.Deviation"].Value, Statistics["Attack.Deviation"].Value), 1, NearestShip.Position.Z + math.random(-Statistics["Attack.Deviation"].Value, Statistics["Attack.Deviation"].Value))
--local MovingRecoil = Vector3.new(NearestShip.Front.WorldCFrame.Position.X + math.random(-Statistics["Attack.Deviation"].Value, Statistics["Attack.Deviation"].Value), 1, NearestShip.Front.WorldCFrame.Position.Z + math.random(-Statistics["Attack.Deviation"].Value, Statistics["Attack.Deviation"].Value))
local NewBulletClone = Bullet:Clone()
local NewBulletCloneVelocity = NewBulletClone.LinearVelocity
-- Movement Speed Anticipation, if ship is moving fires in front instead of center
if EnemyShipSpeed > 35 then
NewBulletClone.Size = Vector3.new(Ship.Size.X / 15, 1, Ship.Size.Z / 10)
NewBulletClone.CFrame = Ship.CFrame
NewBulletClone.CFrame = CFrame.lookAt(NewBulletClone.Position, MovingRecoil)
NewBulletClone.Parent = Folders.Projectiles
NewBulletCloneVelocity.VectorVelocity = Vector3.new(200, 0, 200) * NewBulletClone.CFrame.LookVector
else
NewBulletClone.Size = Vector3.new(Ship.Size.X / 15, 1, Ship.Size.Z / 10)
NewBulletClone.CFrame = Ship.CFrame
NewBulletClone.CFrame = CFrame.lookAt(NewBulletClone.Position, Recoil)
NewBulletClone.Parent = Folders.Projectiles
NewBulletCloneVelocity.VectorVelocity = Vector3.new(200, 0, 200) * NewBulletClone.CFrame.LookVector
end
Statistics["Attack.BulletCount"].Value -= 1
wait(0.25)
else
wait(Statistics["Attack.Speed"].Value)
Statistics["Attack.BulletCount"].Value = Statistics["Attack.BurstFire"].Value
end
end
end
end
end
wait()
end