Hello.
I am making an airplane that is armed with both guns and 20mm cannons
I used task.spawn but its just spewing out bullets without using the wait statements.
local function FireCannons(target)
for _,v in pairs(script.Parent:GetDescendants()) do
if v.Name == "Cannon" then
local b = game:GetService("ReplicatedStorage").Ammo.JC
local bullet = b:Clone()
bullet.Position = v.Position
bullet.Orientation = v.Orientation
bullet.Parent = workspace
wait(0.25)
end
end
end
local function FireGuns(target)
for _,v in pairs(script.Parent:GetDescendants()) do
if v.Name == "MachineGun" then
local b = game:GetService("ReplicatedStorage").Ammo.JB
local bullet = b:Clone()
bullet.Position = v.Position
bullet.Orientation = v.Orientation
bullet.Parent = workspace
wait(0.1)
end
end
end
local function FireBoth()
task.spawn(FireGuns)
task.spawn(FireCannons)
end
How would I do this with the wait statements in the gun and cannons shooters?
local function attack(target)
if target and target.BluePlane and target.Health.Value > 0 then
local origincframe = engine.BodyGyro.cframe
local dir = (script.Parent.RedPlane.Position - target.BluePlane.Position).unit
local spawnPos = script.Parent.RedPlane.Position
local pos = spawnPos + dir
engine:findFirstChild("BodyGyro").maxTorque = Vector3.new(10000,10000,10000)
engine:findFirstChild("BodyGyro").cframe = CFrame.new(pos, pos+dir)
adjustSpeed(target)
FireBoth(target)
BOOM(target)
end
end
not entirely sure, but I would use task.wait() instead of wait(), especially since this is using the task library already. May or may not fix the issue but it’s just an idea.
I think I know the solution for your problem. What task.spawn() does is that it creates a new coroutine using the given function, then fires that coroutine immediatly. Therefore, when you run your code, It won’t delay the code, rather, it will fire rapidly without delay. What you need to do is either move your wait statements outside your Fire() functions or use a debounce.
Try using this:
local CanFireCannons = true
local CanFireGuns = true
local function FireCannons(target)
if CanFireCannons then
CanFireCannons = false
for _,v in pairs(script.Parent:GetDescendants()) do
if v.Name == "Cannon" then
local b = game:GetService("ReplicatedStorage").Ammo.JC
local bullet = b:Clone()
bullet.Position = v.Position
bullet.Orientation = v.Orientation
bullet.Parent = workspace
wait(0.25)
end
end
CanFireCannons = true
end
end
local function FireGuns(target)
if CanFireGuns then
CanFireGuns = false
for _,v in pairs(script.Parent:GetDescendants()) do
if v.Name == "MachineGun" then
local b = game:GetService("ReplicatedStorage").Ammo.JB
local bullet = b:Clone()
bullet.Position = v.Position
bullet.Orientation = v.Orientation
bullet.Parent = workspace
wait(0.1)
end
end
CanFireGuns = true
end
end
local function FireBoth()
task.spawn(FireGuns)
task.spawn(FireCannons)
end