Switch Turret Target Issue

  1. What do you want to achieve? Keep it simple and clear!
    Make turret switch and shoot when it sees zombies

  2. What is the issue? Include screenshots / videos if possible!
    So the issue is that it locks on the first zombie, right, but then it doesn’t react to other zombies.

Video of what I mean:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried using all my knowledge to fix this issue, none of it helped me.

Code:

local PlayerService = game:GetService("Players")
local Turret = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local ts = game:GetService("TweenService")



local Events = ReplicatedStorage:WaitForChild("Events")

local value = script.Parent.TurretScript.Setting:WaitForChild("Value")
local part = script.Parent.Head

local closestEnemy
local closestDistance = 0
value.Value = false


local Range = 70 -- How far the Turret can see in studs.
local FOV = 90 -- The Turret's FOV


local function GetCharacters()
	local Characters = {}

	for _, b in workspace.Zombies:GetChildren() do
		if b.Humanoid.Health <= 0 then continue end
		table.insert(Characters, b)
	end
	return Characters
end


local function CheckLineOfSight(Start, End)
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {Turret, GetCharacters()}
	Params.FilterType = Enum.RaycastFilterType.Exclude

	local Raycast = game.Workspace:Raycast(Start, End - Start, Params)

	if Raycast then
		return false
	else
		return true
	end
end
local closestEnemy
local closestDistance = 0
local function FOVCast()
	value.Value = false
	for _, b in GetCharacters() do
		if b then
			if closestEnemy then
				if closestEnemy.Parent.Humanoid.Health > 0 then
					part.CFrame = CFrame.lookAt(part.Position,closestEnemy.Position)
					value.Value = true
				else
					continue
				end
			else
				local NPCToCharacter = (b.Head.Position - Turret.Head.Position).Unit
				local NPCLookVector = Turret.Head.CFrame.LookVector

				local DotProduct = NPCToCharacter:Dot(NPCLookVector)
				local Angle = math.deg(math.acos(DotProduct))

				local Distance = (Turret.HumanoidRootPart.Position - b.HumanoidRootPart.Position).Magnitude
				if Angle <= FOV and Distance <= Range and --[[Distance < closestDistance and]] CheckLineOfSight(Turret.Head.Position, b.Head.Position) then
					local zombiepart = b.Torso
					closestEnemy = zombiepart
					closestDistance = Distance
					part.CFrame = CFrame.lookAt(part.Position,zombiepart.Position)
					value.Value = true
				else
					value.Value = false
					continue
				end
			end
		end
	end
end
local Casting = coroutine.create(function()
	while task.wait() do
		FOVCast()
	end
end)

coroutine.resume(Casting)

The way it shoots is by changing a variable; if its true, then another script is running.

4 Likes

Try this: Whenever a zombie dies, remove it from the zombies table you created

Updated the code in the main post. Still, It’s the same, it just like it stops doing everything when zombie dies.

After the sentry turret thing kills a zombie, try pausing the courotine for a second or try to forcefully debug it

How in this situation I would detect if zombie is dead?

try naming each one “ZombieRig”, then make a for i loop that detects if there is any other “ZombieRig” in the workspace

If the zombie has a humanoid, which I presume it does, then detect if the hp is <= 0 and if it is then you can tell the code that the zombie is dead

Yeah, I have the GetCharacters() function, which returns all zombies that are alive.
But it still does nothing

Had some free time and since your code was a little messy I rewrote the whole thing. It now uses a heartbeat connection and the code is more optimized and clean. I uploaded a model that you can test out, learn from, or use yourself. It has a turret that switches to the nearest target in its range and field of view. I used this video to calculate the fov: https://www.youtube.com/watch?v=BUwAcW_18Ws. The model link: https://create.roblox.com/marketplace/asset/15621508196/Forum-Example%3Fkeyword=&pageNumber=&pagePosition=

Heres a video of it in action:

Thanks, but I would like to know why and how my code was messy.

Your initial script had a few issues that could be considered as making the code somewhat messy.

Redundant Variable Declarations: In your initial script, you declared closestEnemy and closestDistance variables at the beginning of the script and then again later inside the FOVCast function. It’s generally better to keep variable declarations close to where they are used to avoid confusion.

local closestEnemy
local closestDistance = 0
value.Value = false

Unused Variables: You declared local NPCToCharacter and local NPCLookVector in the FOVCast function, but these variables were not used. Unused variables can make the code seem cluttered.

Inefficient Looping: In the FOVCast function, you looped through GetCharacters() to check line of sight for each zombie. Instead, you can directly iterate over the zombies in GetCharacters() without the need for an additional loop.

Redundant CheckLineOfSight Function: You defined a function CheckLineOfSight for raycasting, but Lua already provides workspace:Raycast that you can use directly. The CheckLineOfSight function was redundant and added unnecessary complexity.

Unused task.wait: In your coroutine, you used task.wait without specifying the time to wait, which could lead to unexpected behavior. If you want to yield for a certain amount of time, you should pass a number to task.wait .

what @progamers246810 solved. he did all of these:

  1. Improved Variable Naming: You used more meaningful variable names, such as TurretHead instead of part.
  2. More Efficient Looping: You simplified the looping logic in the targetZombies function, directly iterating over the zombies without the need for a separate function.
  3. Tweening for Turret Reset: You added a tween for resetting the turret to its original position, providing a smoother transition.
  4. Cleaner Code Structure: You organized the code into functions, making it more modular and easier to read.
1 Like

oh okay, thanks to you and @progamers246810 soo much :smile:

2 Likes

Thanks for explaining (char limit)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.