Cannot make killer retarget the player

Hello there!

I’m currently struggling on working on AI for a killer, this killer has a Tagged system where when a player is within its radius, it will make said player “Tagged” by placing a C4 on them and will ignore the player unless if the player does two things, die by it or survive it in any means such as using a forcefield.

The only issue is that I cannot seem to make the killer target the player again once they are “Untagged” since it keeps returning nil to the players character.

--//NPC
local NPC = script.Parent
local Humanoid = NPC:WaitForChild("Humanoid")
Humanoid.BreakJointsOnDeath = false
NPC.HumanoidRootPart:SetNetworkOwner(nil) --No hacking
--//Services
local Players = game:GetService("Players")
local PFS = game:GetService("PathfindingService")
local Debris = game:GetService("Debris")
--//Pathfinding Stuff
local AttackingRange = Vector3.new(3,3,3)
local CharacterSize = NPC:GetExtentsSize()

local PathParams = {
	AgentRadius = (CharacterSize+CharacterSize)/4,
	AgentHeight = CharacterSize.Y,
	AgentCanJump = true
}

local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
--//AI
local Tagged = {}
local function FindNearestTarget()
	local players = {}
	local nearest = math.huge
	local Target = nil

	for _, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character or Player.CharacterAdded:Wait()

		local Distance = (NPC.HumanoidRootPart.Position - Character.PrimaryPart.Position).Magnitude
		if Distance <= nearest then
			if table.find(Tagged,Player.Name) then
				return Target
			end
			
			table.insert(players,{
				Magnitude = Distance,
				player = Player
			})
		end
		
		if Character:FindFirstChildWhichIsA("Humanoid").Health <= 1 then
			table.remove(Tagged,Player)
		end
	end

	for _, Entry in pairs(players) do
		local Mag = Entry.Magnitude
		local Plr = Entry.player
		if Mag <= nearest then
			nearest = Mag
			Target = Plr
		end
	end
	
	return Target
end

game:GetService("RunService").Heartbeat:Connect(function()
	local Player = FindNearestTarget()
	--//Character
	if NPC == nil or table.find(Tagged,Player) then
		return
	end

	local Player_Char = Player.Character or Player.CharacterAdded:Wait()
	local Char_Root = Player_Char.PrimaryPart
	local Char_Hum = Player_Char:FindFirstChildWhichIsA("Humanoid")

	if Char_Hum.Health <= 1 then
		return
	end

	--//Pathfinding
	local Goal = Char_Root.Position
	local Path = SimplePath.new(NPC)
	Path.Blocked:Connect(function()
		Path:Run(Goal)
	end)
	Path.Error:Connect(function(errorType)
		Path:Run(Goal)
	end)
	Path:Run(Goal)

	if Char_Hum.Health <= 1 then
		table.remove(Tagged,Player.Name)
	end
end)
1 Like

When you add the tag you reference Player.Name, but when you remove you only use Player. Choose one, not both.

Edit: Nvm, I get what you mean. It still doesn’t target the player after said player is untagged but there is no errors whatsoever

Try this code. You used table.find(table,Player) and then added table.remove(table,Player.Name)

--//NPC
local NPC = script.Parent
local Humanoid = NPC:WaitForChild("Humanoid")
Humanoid.BreakJointsOnDeath = false
NPC.HumanoidRootPart:SetNetworkOwner(nil) --No hacking
--//Services
local Players = game:GetService("Players")
local PFS = game:GetService("PathfindingService")
local Debris = game:GetService("Debris")
--//Pathfinding Stuff
local AttackingRange = Vector3.new(3,3,3)
local CharacterSize = NPC:GetExtentsSize()

local PathParams = {
	AgentRadius = (CharacterSize+CharacterSize)/4,
	AgentHeight = CharacterSize.Y,
	AgentCanJump = true
}

local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
--//AI
local Tagged = {}
local function FindNearestTarget()
	local players = {}
	local nearest = math.huge
	local Target = nil

	for _, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character or Player.CharacterAdded:Wait()

		local Distance = (NPC.HumanoidRootPart.Position - Character.PrimaryPart.Position).Magnitude
		if Distance <= nearest then
			if table.find(Tagged,Player.Name) then
				return Target
			end
			
			table.insert(players,{
				Magnitude = Distance,
				player = Player
			})
		end
		
		if Character:FindFirstChildWhichIsA("Humanoid").Health <= 1 then
			table.remove(Tagged,Player.Name)
		end
	end

	for _, Entry in pairs(players) do
		local Mag = Entry.Magnitude
		local Plr = Entry.player
		if Mag <= nearest then
			nearest = Mag
			Target = Plr
		end
	end
	
	return Target
end

game:GetService("RunService").Heartbeat:Connect(function()
	local Player = FindNearestTarget()
	--//Character
	if NPC == nil or table.find(Tagged,Player.Name) then
		return
	end

	local Player_Char = Player.Character or Player.CharacterAdded:Wait()
	local Char_Root = Player_Char.PrimaryPart
	local Char_Hum = Player_Char:FindFirstChildWhichIsA("Humanoid")

	if Char_Hum.Health <= 1 then
		return
	end

	--//Pathfinding
	local Goal = Char_Root.Position
	local Path = SimplePath.new(NPC)
	Path.Blocked:Connect(function()
		Path:Run(Goal)
	end)
	Path.Error:Connect(function(errorType)
		Path:Run(Goal)
	end)
	Path:Run(Goal)

	if Char_Hum.Health <= 1 then
		table.remove(Tagged,Player.Name)
	end
end)

Doesn’t work sadly since it keeps spewing out “Attempt to index nil with “Name””.

Which line throws this error??

Line 64

nxaknaxnksankasdnkasknsnjsa

Try this

--//NPC
local NPC = script.Parent
local Humanoid = NPC:WaitForChild("Humanoid")
Humanoid.BreakJointsOnDeath = false
NPC.HumanoidRootPart:SetNetworkOwner(nil) --No hacking
--//Services
local Players = game:GetService("Players")
local PFS = game:GetService("PathfindingService")
local Debris = game:GetService("Debris")
--//Pathfinding Stuff
local AttackingRange = Vector3.new(3,3,3)
local CharacterSize = NPC:GetExtentsSize()

local PathParams = {
	AgentRadius = (CharacterSize+CharacterSize)/4,
	AgentHeight = CharacterSize.Y,
	AgentCanJump = true
}

local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
--//AI
local Tagged = {}
local function FindNearestTarget()
	local players = {}
	local nearest = math.huge
	local Target = nil

	for _, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character or Player.CharacterAdded:Wait()

		local Distance = (NPC.HumanoidRootPart.Position - Character.PrimaryPart.Position).Magnitude
		if Distance <= nearest then
			if table.find(Tagged,Player.Name) then
				return Target
			end
			
			table.insert(players,{
				Magnitude = Distance,
				player = Player
			})
		end
		
		if Character:FindFirstChildWhichIsA("Humanoid").Health <= 1 then
			table.remove(Tagged,Player.Name)
		end
	end

	for _, Entry in pairs(players) do
		local Mag = Entry.Magnitude
		local Plr = Entry.player
		if Mag <= nearest then
			nearest = Mag
			Target = Plr
		end
	end
	
	return Target
end

game:GetService("RunService").Heartbeat:Connect(function()
	local Player = FindNearestTarget()
	if Player == nil then
	   return
	end
	--//Character
	if NPC == nil or table.find(Tagged,Player.Name) then
		return
	end

	local Player_Char = Player.Character or Player.CharacterAdded:Wait()
	local Char_Root = Player_Char.PrimaryPart
	local Char_Hum = Player_Char:FindFirstChildWhichIsA("Humanoid")

	if Char_Hum.Health <= 1 then
		return
	end

	--//Pathfinding
	local Goal = Char_Root.Position
	local Path = SimplePath.new(NPC)
	Path.Blocked:Connect(function()
		Path:Run(Goal)
	end)
	Path.Error:Connect(function(errorType)
		Path:Run(Goal)
	end)
	Path:Run(Goal)

	if Char_Hum.Health <= 1 then
		table.remove(Tagged,Player.Name)
	end
end)

Still doesn’t retarget the player after they are untagged via death, but no errors are shown.

I am still unsure what Tagged means in ur script. But try this.

--//NPC
local NPC = script.Parent
local Humanoid = NPC:WaitForChild("Humanoid")
Humanoid.BreakJointsOnDeath = false
NPC.HumanoidRootPart:SetNetworkOwner(nil) --No hacking
--//Services
local Players = game:GetService("Players")
local PFS = game:GetService("PathfindingService")
local Debris = game:GetService("Debris")
--//Pathfinding Stuff
local AttackingRange = Vector3.new(3,3,3)
local CharacterSize = NPC:GetExtentsSize()

local PathParams = {
	AgentRadius = (CharacterSize+CharacterSize)/4,
	AgentHeight = CharacterSize.Y,
	AgentCanJump = true
}

local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
--//AI
local Tagged = {}
local function FindNearestTarget()
	local players = {}
	local nearest = math.huge
	local Target = nil
	for _, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character or Player.CharacterAdded:Wait()

		local Distance = (NPC.HumanoidRootPart.Position - Character.PrimaryPart.Position).Magnitude
		if Distance <= nearest then
			if not table.find(Tagged,Player.Name) then
			    table.insert(players,{
			    	Magnitude = Distance,
				    player = Player
			    })
			end
		end
		
		if Character:FindFirstChildWhichIsA("Humanoid").Health <= 1 then
			table.remove(Tagged,Player.Name)
		end
	end

	for _, Entry in pairs(players) do
		local Mag = Entry.Magnitude
		local Plr = Entry.player
		if Mag <= nearest then
			nearest = Mag
			Target = Plr
		end
	end
	
	return Target
end

game:GetService("RunService").Heartbeat:Connect(function()
	local Player = FindNearestTarget()
	if Player == nil then
	   return
	end
	--//Character
	if NPC == nil or table.find(Tagged,Player.Name) then
		return
	end

	local Player_Char = Player.Character or Player.CharacterAdded:Wait()
	local Char_Root = Player_Char.PrimaryPart
	local Char_Hum = Player_Char:FindFirstChildWhichIsA("Humanoid")

	if Char_Hum.Health <= 1 then
		return
	end

	--//Pathfinding
	local Goal = Char_Root.Position
	local Path = SimplePath.new(NPC)
	Path.Blocked:Connect(function()
		Path:Run(Goal)
	end)
	Path.Error:Connect(function(errorType)
		Path:Run(Goal)
	end)
	Path:Run(Goal)

	if Char_Hum.Health <= 1 then
		table.remove(Tagged,Player.Name)
	end
end)

I’m creating a Tagged system for this killer where the killer will not target a player considered “Tagged”. You can imagine it being a killer inflicting a permanent bleed effect onto the victim and leaving the victim to die.

In order for the killer to make players tagged, he must be within range where the killer will attach a C4 onto the player and ignore them unless two things happen; the player dies or the player survives with any means such as forcefields. If either the two circumstances are met, the player is untagged and the killer will target them again.

Ok, now try the script, i changed it a bit.

Still nothing, but an error saying “invalid argument #2 to ‘remove’ (number expected, got string)”

Which line do you get this error?

42
dsnjqdhwiadsaikjkjkjkjwqekewqhuieqho8qewhhiquq

Ok, i tried to fix it again… I fixed the line 42!

--//NPC
local NPC = script.Parent
local Humanoid = NPC:WaitForChild("Humanoid")
Humanoid.BreakJointsOnDeath = false
NPC.HumanoidRootPart:SetNetworkOwner(nil) --No hacking
--//Services
local Players = game:GetService("Players")
local PFS = game:GetService("PathfindingService")
local Debris = game:GetService("Debris")
--//Pathfinding Stuff
local AttackingRange = Vector3.new(3,3,3)
local CharacterSize = NPC:GetExtentsSize()

local PathParams = {
	AgentRadius = (CharacterSize+CharacterSize)/4,
	AgentHeight = CharacterSize.Y,
	AgentCanJump = true
}

local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
--//AI
local Tagged = {}
local function FindNearestTarget()
	local players = {}
	local nearest = math.huge
	local Target = nil
	for _, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character or Player.CharacterAdded:Wait()

		local Distance = (NPC.HumanoidRootPart.Position - Character.PrimaryPart.Position).Magnitude
		if Distance <= nearest then
			if not table.find(Tagged,Player.Name) then
			    table.insert(players,{
			    	Magnitude = Distance,
				    player = Player
			    })
			end
		end
		
		if Character:FindFirstChildWhichIsA("Humanoid").Health <= 1 then
		    pcall(function()
				table.remove(Tagged,Player.Name)
		   end)
		end
	end

	for _, Entry in pairs(players) do
		local Mag = Entry.Magnitude
		local Plr = Entry.player
		if Mag <= nearest then
			nearest = Mag
			Target = Plr
		end
	end
	
	return Target
end

game:GetService("RunService").Heartbeat:Connect(function()
	local Player = FindNearestTarget()
	if Player == nil then
	   return
	end
	--//Character
	if NPC == nil or table.find(Tagged,Player.Name) then
		return
	end

	local Player_Char = Player.Character or Player.CharacterAdded:Wait()
	local Char_Root = Player_Char.PrimaryPart
	local Char_Hum = Player_Char:FindFirstChildWhichIsA("Humanoid")

	if Char_Hum.Health <= 1 then
		return
	end

	--//Pathfinding
	local Goal = Char_Root.Position
	local Path = SimplePath.new(NPC)
	Path.Blocked:Connect(function()
		Path:Run(Goal)
	end)
	Path.Error:Connect(function(errorType)
		Path:Run(Goal)
	end)
	Path:Run(Goal)

	if Char_Hum.Health <= 1 then
		table.remove(Tagged,Player.Name)
	end
end)

dammit still nothing. Might as well scrap the killer entirely. Thanks for the help.

njsjnasnjasdjnasdjndsjnsadj