Is this tower attack system okay?

Hey!
This is a system I made completely by myself, and usually I make some small issues. This is something I’m proud of, and was wondering if it can be improved.
Thanks!

--This is a module btw
local TowerModule = {}

function TowerModule.SingleAttack(TowerModel)
	coroutine.wrap(function()
		local MainPart = TowerModel.MainPart
		local CurrentLevel = TowerModel.CurrentLevel

		local Levels = TowerModel.Levels
		local CurrentLevel = Levels[CurrentLevel.Value]

		local TargetMode = CurrentLevel.TargetMode
		local Target = TowerModel.CurrentTarget

		local Map = game:GetService("ReplicatedStorage").SelectedMap

		if TargetMode.Value == 1 then
			for index, zombie in pairs(Map.Value.RunningZombies:GetChildren()) do
				local HRP = zombie.HumanoidRootPart
				local MagnitudeFromMainPart = (MainPart.Position - HRP.Position).Magnitude
				local MagnitudeFromEndPoint = (HRP.Position - Map.Value.Path.EndPoint.Position).Magnitude

				if Target.Value then
					local CurrentMagnitudeFromMainPart = (MainPart.Position - Target.Value.HumanoidRootPart.Position).Magnitude
					local CurrentMagnitudeFromEndPoint = (Target.Value.HumanoidRootPart.Position - Map.Value.Path.EndPoint.Position).Magnitude
					if CurrentMagnitudeFromEndPoint > MagnitudeFromEndPoint then
						Target.Value = zombie
					end
				elseif MagnitudeFromMainPart <= CurrentLevel.Range.Value then
					Target.Value = zombie
				end
			end
			if Target.Value then
				Target.Value.Humanoid:TakeDamage(CurrentLevel.Damage.Value)
			end
			if Target.Value then
				if Target.Value.Parent ~= workspace then
					Target.Value = nil
				else
					warn(Target.Value.Name, "Is still in workspace!")
				end
			end
		end
	end)()
end

return TowerModule

2 Likes

Hey, there.
Can you show some screenshots or some videos of your system?

1 Like

robloxapp-20220215-0936321.wmv (5.5 MB)

1 Like

The system is working great. Nice job btw. Hope you will create some great games! Good luck working on it!!

2 Likes

Thanks!
One issue is occasionally it hits the closest once then returns to normal, but this isn’t really major. Is it possible to be fixed? I don’t mind it much.

1 Like

I am not a great scripter, lol. I just saw your work, and was impressed.

1 Like

What do you mean by that, could you please elaborate?

It shoots at the target who isn’t the first. This only happens once every so often though.