Task.spawn Help please!

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?

Put the wait() inside the FireBoth function.
What’s happening is that it’s running the function without a wait().

(I think)

1 Like

@BriefYayyayyay.
Thanks but the guns have different fire rates.

What fires the fireboth function?

1 Like
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

attack is being run by a while true loop
@BriefYayyayyay

What if you make that twice but on one replace FireBoth(target) with:

FireCannons(target)
wait(0.25)

And the other:

FireGuns(target)
wait(0.1)

Would that work?

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.

Thanks, trying it out now.

Nope.

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
1 Like

Thanks, it worked. Imma go actually learn the stuff instead of copy paste completely lol. :smiley: Now my A6M Zero works. :smiley:

1 Like