One cannon works but not two

Heyo Devforum! Today I have been trying out my newly made cannons, but after placing two down, I noticed how one never fired but the other did. I tried this with multiple cannons more and there was the same result. Only one cannon fired. I am really confused by this because my script gets all of the cannons and makes them fire, but it is only getting one. I tried switching up the script here and there, but nothing worked. If there is something that is wrong with my script, please let me know!

Script:

repeat wait() until game.Workspace:WaitForChild("Tower of Troubling Tutorials")

local cannons1 = game.Workspace["Tower of Troubling Tutorials"].Cannons:GetChildren()

for i, cannon in pairs(cannons1) do
	local barrel = cannon.Barrel
	local ball = game.ReplicatedStorage.CanonBal
	while true do
		local ballclone = ball:Clone()
		ballclone.Parent = game.Workspace["Tower of Troubling Tutorials"].Cannons.CanonBalStorage
		ballclone.Position = barrel.Position
		ballclone.Velocity = Vector3.new(0,0,75)
		ballclone.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				local char = player.Character
				local explosion = Instance.new("Explosion")
				explosion.Parent = game.Workspace["Tower of Troubling Tutorials"].Cannons.CanonBalStorage
				explosion.Position = char.HumanoidRootPart.Position
				ballclone:Detroy()
			else
				wait(3)
				ballclone:Destroy()
			end
		end)
		wait(5)
	end
end

P.S. there are no errors

One possibility is that when the script gets all the cannons, it only gets the cannons that have loaded. Try adding a wait before the scripts gets the cannons, and see if that helps.

1 Like

Maybe I didn’t do it right, but I added a wait(1) before the cannons1 variable. When I tested it still had the same result. Would I put the wait somewhere else?

The while loop causes the for loop to never continue. It is stuck at the first cannon.

Fix it by passing the while loop to the spawn() function, or you can create a separate script that controls a cannon, and clone that script into each cannon.

2 Likes

it might just be that you have to wait for one canon ball to fully fire and be destroyed and the 5 second wait but i may be wrong, it also is infinitely repeating in the while loop as the person above me has said

1 Like

Yeah, weekly shoes is right. The for loop has to completely loop through each cannon before it continues. The While Loop never ends so the next cannon doesn’t fire. I would make a function at the top of the script, and use courortines to call that function.

1 Like

In the line before the else you have “ballclone:Detroy()”, should be Destroy.
Dunno if that affects what’s going on though.

1 Like

Try this. I basically just changed the code around. Every 5 seconds it loops through all the cannons and fires the cannon balls.

repeat wait() until game.Workspace:WaitForChild("Tower of Troubling Tutorials")

local cannons1 = game.Workspace["Tower of Troubling Tutorials"].Cannons:GetChildren()


while true do
		wait(5)
for i, cannon in pairs(cannons1) do
	local barrel = cannon.Barrel
	local ball = game.ReplicatedStorage.CanonBal
		local ballclone = ball:Clone()
		ballclone.Parent = game.Workspace["Tower of Troubling Tutorials"].Cannons.CanonBalStorage
		ballclone.Position = barrel.Position
		ballclone.Velocity = Vector3.new(0,0,75)
		ballclone.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				local char = player.Character
				local explosion = Instance.new("Explosion")
				explosion.Parent = game.Workspace["Tower of Troubling Tutorials"].Cannons.CanonBalStorage
				explosion.Position = char.HumanoidRootPart.Position
				ballclone:Detroy()
			else
				wait(3)
				ballclone:Destroy()
			end
		end)
end
end
4 Likes

Thank you for all of your answers, however, WeeklyShoe provided me with the solution. I totally forgot how I can thread this with spawn! Once again, thank you for helping me everyone!

1 Like