An aircraft needs to return near its carrier to resupply weapons/ordnance.
I am using RunService to check if the plane is at resupply distance or not. Most times it works, but there are a few times that the plane doesn’t resupply. I am also using RunService to check if the plane is below sea level (Y: 0) and if so, it explodes. Sometimes this doesn’t work as well when the plane isn’t able to resupply.
Is there a reason why RunService won’t work? Should I be using a different function to check for these positions?
game:GetService("RunService").Heartbeat:Connect(function()
if exploded == false then
if script.Parent.PrimaryPart.Position.Y < 0 then
boom() --explosion function
end
--below checks if aircraft is near its carrier
if ((workspace.Antares["Aircraft Carrier Scorpius Alpha"].Carrier.Ship.ResupplyRef.Position - script.Parent.PrimaryPart.Position)).Magnitude < resupplydistance then
for _,o in pairs(script.Parent.Weapons.Bombs:GetChildren()) do
o.MeshPart.Transparency = 0 --show weapons to indicate it's resupplied
end
resupplied = true --enables weapon usage
end
end
end);
It looks like the event listener you have checks if ‘exploded’ is false before doing anything else. I assume the ‘boom’ function is responsible for setting this flag. Is there anything else that might cause the script to think the airplane is dead when it isn’t?
I’m conscious that I shouldn’t bump this since it’s not directly concerning your issue, but I wouldn’t suggest performance-wise performing such complex operations in a Heartbeat event as it will consume a lot of resource. To optimize it at low scale, you could use return while calling boom() to avoid looping through the children.
Alternatively, you could use ToObjectSpace() to determine if the aircraft is flying over the aircraft carrier, you could write something similar to this :
Might not be accurate for your specific case
function check()
local cf = middleOfAircraftCarrierCF:ToObjectSpace(aircraftCf)
local span = 50 -- studs
-- Assuming Z corresponds to the span of the AC (aircraft carrier), we can simply
-- compare the values
if cf.Z > span and cf.Z < span then
-- inbound
else
-- out of range for the resupply
end
-- We could also take in consideration the altitude (Y pos) and various other factors, but I guess you
-- could achieve that yourself!
end
To conclude, I can’t really provide efficient help without the context, I would strongly recommend to rework the structure of your code, to avoid numerous problems especially with exploiters and lag.
also your game is quite nice and fun when there’s no clowns to ruin it