I think its about time i asked the community for help. I've searched the dev forum and everywhere for answers, and have found none.

So as the title suggests, I need help with my towers. I have a somewhat accurate targeting system, and the script just errors when i made it kill enemies. Right now, when the enemy get out of range, it doesn't always switch to the first enemy. Currently my targeting system uses something I found on the devforum. Basically, each enemy has a Pos Value which increases depending on the speed. The tower then finds the one with the highest value in its range, and then sets it to the target. I'm not sure why it isn't working, so any help is appreciated. The problems with the attacking is when the enemy is destroyed, the script can't find a target and completely errors.

Here is the targeting script:

Turret = script.Parent
Range = Turret.RangeBubble
EnemyFolder = game.Workspace.Enemies
debounce = false
local CurrentTarget
while true do
	local enemies = game.Workspace.Enemies:GetChildren()
		wait(0.001)
	for i, Enemy in pairs (enemies) do
		if Enemy:IsDescendantOf(game.Workspace.Enemies) == true then
			if (Turret.BulletStart.Position - Enemy.HumanoidRootPart.Position).magnitude < Turret.Configuration.Range.Value  then
				PositionNumber = Enemy.PositionNumber
				BeingTargetted = Enemy.BeingTargetted.Value
				if CurrentTarget == nil or PositionNumber.Value > CurrentTarget.PositionNumber.Value then
					if (Turret.BulletStart.Position - Enemy.HumanoidRootPart.Position).magnitude < Turret.Configuration.Range.Value then
						
							CurrentTarget = Enemy
						
					end
				end
				if (Turret.BulletStart.Position - CurrentTarget.HumanoidRootPart.Position).magnitude > Turret.Configuration.Range.Value then
					CurrentTarget = nil
				end
				Enemy.BeingTargetted.Value = true
				Turret.Targetting.Value = CurrentTarget
			end
		end
    end
end

The attacking script(without the damage):

Turret = script.Parent

while true do
	Targetting = Turret.Targetting
	Snake = Targetting.Value
	wait(0.01)
	if Snake ~= nil then
		HRP = Snake.HumanoidRootPart
		Beam = Instance.new("Part",workspace)
		Beam.Size = Vector3.new(0.5,0.5,(Turret.BulletStart.Position - HRP.Position).Magnitude)
		Beam.CFrame = Turret.BulletStart.CFrame:Lerp(HRP.CFrame, 0.5)
		Beam.CFrame = CFrame.new(Beam.Position, HRP.Position)
		Beam.Anchored = true
		Beam.CanCollide = false
		Beam.BrickColor = BrickColor.new("Bright yellow")
		wait(0.01)
		Beam:Destroy()
		Turret.TurningPart.CFrame = CFrame.new(Turret.TurningPart.Position, HRP.Position)
	end
end

Note that my enemies use humanoids. This is something i don't want or plan to change. I would prefer not changing it.

1 comment

0

No one? No?