So I’ve made a Star Destroyer fight a CR90 Corvette (Tantive IV!) and vice versa but the Star Destroyer starts shooting when it’s health goes to 0. I’ve tried looking everywhere in my script but was stumped.
local bullet = game.ReplicatedStorage.Laser
local turret = script.Parent
local fireRate = .75
local Damage = 3
local speed = 1500
local aggroDist = 7500
while wait(fireRate) do
local target = nil
for i, v in pairs(workspace:GetChildren()) do
local side = v:FindFirstChild("Side")
local health = v:FindFirstChild("Health")
local part = v:FindFirstChild("Primary")
if (health and health.Value > 0) and (side and side.Value == "Rebellion") and part then
if (part.Position - turret.Position).magnitude < aggroDist then
local bulletRay = Ray.new(turret.Position, (part.Position - turret.Position).Unit * 250000)
local hit, position = workspace:FindPartOnRayWithIgnoreList(bulletRay, {turret})
if hit == part then
target = part
--print("Object not in the way :)")
else
--print("Object in the way :(")
--print(tostring(hit), tostring(hit.Parent), tostring(hit.Parent.Parent))
end
end
end
end
if target then
local part = target
-- turret.CFrame = CFrame.new(turret.Position, part.Position)
local newBullet = bullet:Clone()
newBullet.Position = turret.Position
newBullet.Parent = workspace
newBullet.Velocity = turret.CFrame.LookVector * speed
newBullet.Touched:Connect(function(hit)
if hit.Name ~= "Primary" then
local health = hit.Parent:FindFirstChild("Health")
if health then
if health.Value > 0 then
health.Value = health.Value - Damage
newBullet:Destroy()
if health.Value == 0 then
for _, part in pairs(hit.Parent:GetDescendants()) do
if part:IsA("WeldConstraint") then
part:Destroy()
print("DESTROYED")
elseif part:IsA("BasePart") then
part.Anchored = false
print("UN-ANCHORED")
end
end
end
end
end
end
end)
end
end
So I found why this is happening. I added an else statement which would lead to a warning.
while wait(fireRate) do
local target = nil
for i, v in pairs(workspace:GetChildren()) do
local side = v:FindFirstChild("Side")
local health = v:FindFirstChild("Health")
local part = v:FindFirstChild("Primary")
if (health and health.Value > 0) and (side and side.Value == "Rebellion") and part then
if (part.Position - turret.Position).magnitude < aggroDist then
local bulletRay = Ray.new(turret.Position, (part.Position - turret.Position).Unit * 250000)
local hit, position = workspace:FindPartOnRayWithIgnoreList(bulletRay, {turret})
if hit == part then
target = part
--print("Object not in the way :)")
else
--print("Object in the way :(")
--print(tostring(hit), tostring(hit.Parent), tostring(hit.Parent.Parent))
end
end
else
warn("Something is wrong")
end
end
if target then
local part = target
-- turret.CFrame = CFrame.new(turret.Position, part.Position)
local newBullet = bullet:Clone()
newBullet.Position = turret.Position
newBullet.Parent = workspace
newBullet.Velocity = turret.CFrame.LookVector * speed
newBullet.Touched:Connect(function(hit)
if hit.Name ~= "Primary" then
local health = hit.Parent:FindFirstChild("Health")
if health then
if health.Value > 0 then
health.Value = health.Value - Damage
newBullet:Destroy()
if health.Value == 0 then
for _, part in pairs(hit.Parent:GetDescendants()) do
if part:IsA("WeldConstraint") then
part:Destroy()
print("DESTROYED")
elseif part:IsA("BasePart") then
part.Anchored = false
print("UN-ANCHORED")
end
end
end
end
end
end
end)
else
warn("No target")
end
end
Me being dumb
So I guess either there is no part, side, or health, even though they are shown in the explorer:
Actually, I think what is happening is that there is the health, part, and side, but health is not over 0 which is weird.
I added another else statement here as you could see:
if target then
local part = target
-- turret.CFrame = CFrame.new(turret.Position, part.Position)
local newBullet = bullet:Clone()
newBullet.Position = turret.Position
newBullet.Parent = workspace
newBullet.Velocity = turret.CFrame.LookVector * speed
newBullet.Touched:Connect(function(hit)
if hit.Name ~= "Primary" then
local health = hit.Parent:FindFirstChild("Health")
if health then
if health.Value > 0 then
health.Value = health.Value - Damage
newBullet:Destroy()
if health.Value == 0 then
for _, part in pairs(hit.Parent:GetDescendants()) do
if part:IsA("WeldConstraint") then
part:Destroy()
print("DESTROYED")
elseif part:IsA("BasePart") then
part.Anchored = false
print("UN-ANCHORED")
end
end
end
end
end
end
end)
else
warn("No target")
end
Now that warning came to the output so there is no target.
I believe the problem would be here. I’m assuming that health is a Number or IntValue, which would mean it won’t act like a humanoid’s health, and can drop below 0.
If the turret’s base health isn’t a multiple of 3, then the health value will become less than 0, and so the if statement for 0 health wouldn’t be met.
Prints are your best way to debug this.
if you haven’t done so I’d try adding a print after this line
if (health and health.Value > 0) and (side and side.Value == "Rebellion") and part then
and see if it’s printing after it’s health gets to 0.
Edit: Also, are you truly checking the ship’s health in this script, and if you don’t mind my asking, where exactly? I see you’re looping through and checking health of objects, but I don’t know if you are truly ever checking the health of the ship.
Sorry for the late reply, but I’m checking if the ENEMY ship’s health is over 0 in the scripts. So a Star Destroyer would check the Corvette’s health. I’ll probably add a statement for if the ship the script is in’s (bad grammar, sorry) health.