Code review on turrent module

local bulletModule = require(script.Parent.Bullet)

local RunService = game:GetService("RunService")

local turrentsFolder = workspace.Turrents:GetChildren()

local module = {}

local debounce = nil

function module.moveTurrent(target, humanoid)
	
	humanoid:GetPropertyChangedSignal("Health"):Connect(function()
		if humanoid.Health <= 0 and debounce then
			debounce = nil
		end
	end)
	
	for _, turrent in ipairs(turrentsFolder) do
		
		if turrent and target and humanoid and not debounce then
			
			debounce = true
			
			connection = RunService.Heartbeat:Connect(function()
				if debounce then
					turrent.Gun.CFrame = CFrame.new(turrent.Gun.Position, target.Position)
				else
					connection:Disconnect()
				end
			end)
			
			while wait(1) and debounce do
				bulletModule.shootBullet(turrent.Gun)
			end
		else
			debounce = nil
		end
	end
end

return module

My turrent module above, I’m all ears.

Make sure to disconnect the heartbeat event when you return false.

1 Like
	connection = RunService.Heartbeat:Connect(function()
				if debounce then
					turrent.Gun.CFrame = CFrame.new(turrent.Gun.Position, target.Position)               else
                    connection:Disconnect()
				end
			end)

Thanks, implemented it.