Trouble when finding a new player to target

I’ll get straight forward.
Here’s how I replicated this issue.
One person dies or gets flung out of the map and gets removed by the game.
The turret is stuck trying to find a new target, even though they are right there.


Here’s the code.

local WeaponProperties = {
	['Damage'] = 20,
	['FireRate'] = 0.09,
	['Speed'] = 2000,
	['Distance'] = 1000,
}
local A3 = nil
local A1 = nil
local bool = true
local Base = script.Parent.Base
	local fol
	if not workspace:FindFirstChild(script.Parent.Placer.Value..'AAViperDebris') then
		local b = Instance.new("Folder")
		b.Name = script.Parent.Placer.Value..'AAViperDebris'
		b.Parent = workspace
		fol = b
end
while wait(1) do
	local b = workspace:GetDescendants()
	b = workspace:GetDescendants()
	for _,_Q1 in pairs(b) do
		local A2 = _Q1:FindFirstChild('Humanoid')
		if A2 then
			if (A2.Parent['Torso'].Position - Base.Position).magnitude < WeaponProperties.Distance then
				A1 = A2.Parent['Torso']
			else
				A1 = nil
			end
		end
	end
	while wait(WeaponProperties.FireRate) do
			if A1 ~= nil then
			if A1 == nil or A1.Parent == nil then
				wait(4)
				repeat
					wait()
					print('Attempting')
					local b = workspace:GetDescendants()
	b = workspace:GetDescendants()
	for _,_Q1 in pairs(b) do
		local A2 = _Q1:FindFirstChild('Humanoid')
					if A2 then
						if not A2:FindFirstChild('Torso') then
							-- nothing
						else
							if (A2.Parent['Torso'].Position - Base.Position).magnitude < WeaponProperties.Distance then
				A1 = A2.Parent['Torso']
			else
				A1 = nil
			end
						end
		end
	end
				until A1 ~= nil
					else
				if A1.Parent['Humanoid'].Health == 0 then
					
				else
					local Person = A1.Parent['Torso']
		Base.CFrame = CFrame.new(Base.Position, Person.Position)
		local Decision = math.random(1,2)
		local fuse = 'Arm'..Decision
		local Bullet = Base[fuse].ViperBullet:Clone()
		Bullet.Parent = fol
		Bullet.Transparency = 0
		Bullet.c:Destroy()
		Bullet.Velocity = Base.CFrame.LookVector * WeaponProperties.Speed
		local d = game:GetService('Debris')
		d:AddItem(Bullet,5)
		Bullet.Touched:connect(function(part)
		   if game.Players:FindFirstChild(part.Parent.Name) then
					    part.Parent['Humanoid']:TakeDamage(WeaponProperties.Damage)
				else
					
					if part.Parent:FindFirstChild('Humanoid') then
						part.Parent['Humanoid']:TakeDamage(WeaponProperties.Damage)
					end
		   end
			if part.Name == 'ViperBullet' or part.Name == 'Arm1' or part.Name == 'Arm2' then
					-- nothing
					else
					Bullet:Destroy()	
			end
					end)
				end
			end
		end
	end
end

Suggestions and Solutions are greatly appreciated.
P.S: I don’t have a specific solution I was looking for, so I have to go to the devforum.
Some of the dummies die because of misfire, other than that then they are alive.

While I am not entirely sure how to fix your issues (primarily because it is 4 AM for me currently), I can possibly guide you to this article on raycasting which may help you find your solution.

What raycasting in basic terms is that an invisible line is drawn from a start point to a base part, in this case it would be a line from the turret to the NPC. I think this could possibly help you find your solution.

You could also make the turret fire AFTER the raycast has hit a part.

ARTICLE:

Apologies if this doesn’t help, but hopefully this can at the least guide you in the right direction! I have seen this being used with long-ranged weapons, so maybe it may? Good luck!

Maybe because when the first person dies the turret is not assigned to anything, as you put blank code block there

if A1.Parent['Humanoid'].Health == 0 then

else

Try to set A1 = nil there

That was put blank for a reason.
It’s looking for an enemy that is alive, and within the turret’s range of attack.
If A1 was nil, it will not do anything.
The code was designed to

  1. Check the workspace for players.
  2. See if they are within the turrets range.
  3. Fire at the person, until their HP reaches 0.
  4. Repeat the process.
    It’s doing Step 1, and Step 2, but it’s not doing Step 3.

Raycasting is practically useless as this is not what it was intended to do.

Also, I had a lot of issues with raycasting, so I may not use it.

1 Like

For your initial target you use a loop to wait until the target is in range. After it finds the target, that code is never run again. You could put that into a method and rerun the code. Also, instead of looping through every item in the workspace, you could just loop through players and check to see if they’re in range.

I decided to divide my script into 3 scripts.
Each script plays a part.
Now it finds a new player to target after one is dead or removed by the game.
Also, what if I have enemy NPCS in my game? they would be un-targetable.