Code review needed on turrent module

So here is my module for handling the turrent and so far, looks good. However, I do think there may be some inefficient lines of code.

I want the if statements and the heatbeat loop to be reviewed.

local RunService = game:GetService("RunService")

local module = {}

function module.moveTurrent(object, target, humanoid)
	
	local debounce = nil
	
	if object.PrimaryPart and target and humanoid and humanoid:GetState() ~= not Enum.HumanoidStateType.Dead and not debounce then
		
		 debounce = true	
		
		 if not target or humanoid and debounce then
			 warn("HumanoidRootPart or Humanoid nil")
			 debounce = nil
		 end		
		
		humanoid.Died:Connect(function()
			  if debounce then
				 debounce = nil
			 end
		 end)	
		
		 connection = RunService.Heartbeat:Connect(function()
			if debounce then
				object:SetPrimaryPartCFrame(CFrame.new(object.PrimaryPart.Position,  target.Position))
			else
				connection:Disconnect()
			end
		end)
	end
end

return module 

If you use the body mover “Body Gyro” you can make it accelerate slower or faster cause in real life tanks don’t accelerate at the rate of the large hadron collider :rofl:

1 Like