How to make npc track another player after player's death

I scripted an “Event” where it spawn an ufo and aliens spawn on the map, the aliens go after players but when the last player is dead or if its one player and that player dies the aliens go at the place where the player died and start jittering, and it lags a lot, is there any way for me to stop them from jittering and creating this much lag?

PS: each alien has it’s own script and it lags bc the AssemblyLinearVelocity value changes a lot of times in a short time

Event script:

while wait(10) do
	game.Lighting.ClockTime = 4
	local ufo = game.ReplicatedStorage.UFO:Clone()
	ufo.Parent = workspace
	game.ReplicatedStorage.UFOEvent.Aliens:FireAllClients()
	local count = 0
	repeat 
		wait(1)
		local alienclone = game.ReplicatedStorage.alien:Clone()
		alienclone.Parent = workspace
		alienclone.Script.Enabled = true
		local xpos = math.random(-1700,1700)
		local zpos = math.random(-1700,1700)
		alienclone:MoveTo(Vector3.new(xpos,alienclone.PrimaryPart.Position.Y,zpos))
		count += 1
	until count == 15
	wait(300)
	game.Lighting.ClockTime = 14
	ufo:Destroy()
	for i,v in workspace:GetChildren() do
		if v.Name == "alien" then
			v:Destroy()
		end
	end
	game.ReplicatedStorage.UFOEvent.StopAliens:FireAllClients()
end

Alien script:

wait(0.2)
local npc = script.Parent

local listofplrs = {}


for i,v in game.Players:GetChildren() do
	table.insert(listofplrs, v)
end


if listofplrs ~= {} then
	local randomNum = math.random(1,#listofplrs)
	local char = listofplrs[randomNum].Character
	game["Run Service"].Stepped:Connect(function()
		if char and listofplrs ~= {} then
				npc.PrimaryPart.AssemblyLinearVelocity = ((char.PrimaryPart.Position-npc.PrimaryPart.Position) + Vector3.new(0,18,0)).Unit*40
				npc.PrimaryPart.Touched:Connect(function(otherPart)
					if otherPart.Parent:FindFirstChild("Humanoid") then
						local plr = game.Players:GetPlayerFromCharacter(otherPart.Parent)
						if plr.leaderstats.Size.Value >= 1000 then
							plr.leaderstats.Size.Value -= 1000
							game.ReplicatedStorage.UFOEvent.AfterDeath:FireAllClients()
							Do()
						npc:Destroy()
						else
							otherPart.Parent:FindFirstChild("Humanoid").Health = 0
							game.ReplicatedStorage.UFOEvent.AfterDeath:FireAllClients()
							Do()
						npc:Destroy()
						end
					end
				end)	
		end
	end)
end	
1 Like

In your code, you were connecting npc.PrimaryPart.Touched every frame which caused the lag, and only getting the list of players once after the npc spawned, which prevented the npc from targeting new players who joined.

Here is the rewritten version:

local RS = game:GetService("RunService")

local NPC = script.Parent

local CurrentTarget
local function GetNearestTarget() -- gets the nearest player
	local target
	local distance = 500 -- the npc aggro distance(set it to your liking)
	for _,v in ipairs(game.Players:GetPlayers()) do
		if not v.Character or not v.Character.Parent or v.Character.Humanoid:GetState() == "Dead" then continue end -- making sure theyre not dead and still exists
		
		local distance_from_npc = (v.Character.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Magnitude
		if distance_from_npc < distance then
			distance = distance_from_npc -- getting the nearest target by setting the distance from this player so the next player needs to be this close for the npc to target
			target = v.Character
		end
	end
	return target
end

RS.Stepped:Connect(function()	
	CurrentTarget = CurrentTarget or GetNearestTarget() -- if theres a current target, set it back to that one else it will get a new one
	if CurrentTarget and CurrentTarget.Parent and CurrentTarget.PrimaryPart and CurrentTarget.Humanoid:GetState() ~= "Dead" then -- making sure the target is not dead and exists
		-- if theres a current target and its alive, move to its position
		NPC.Humanoid:MoveTo(CurrentTarget.HumanoidRootPart.Position)
	else
		-- if the target is dead or gone, set it to nil so the next stepped will get a new target
		CurrentTarget = nil
	end
end)

NPC.PrimaryPart.Touched:Connect(function(part)
	-- your code here
end)

Hopefully, this helps!

1 Like

Thank you so much I was so tired of this bug, i modified you code a bit and it works now, the thing is that i cant use move to bc my npc is not a normal npc its just a sphere part that’s why i was using liniar velocity! Have a great day

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