Script wont make NPC's stop killing owner

Hello everyone, i want a script where the NPC’s attack normal players but not the owner.
i have tried many things but wont work.
here is my code:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local furnite = script.Parent
local root = furnite.Parent.PrimaryPart

local targetDistance = script:GetAttribute("TargetDistance")
local stopDistance = script:GetAttribute("StopDistance")
local damage = script:GetAttribute("Damage")
local attackDistance = script:GetAttribute("AttackDistance")
local attackWait = script:GetAttribute("AttackWait")
local lastAttack = tick()

function findNearestPlayer()
	local playerList = Players:GetPlayers()
	
	local nearestPlayer = nil
	local distance = nil
	local direction = nil
	local userIDforOwner = "13140269348"
	
	for _, player in pairs(playerList) do
		local character = player.Character
		if character then
			local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
			if not nearestPlayer then
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit
			elseif distanceVector.Magnitude < distance then
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit
				if nearestPlayer.UserId == userIDforOwner then
					return nil
				end
					
					
			end	
		end
	end
	
	return nearestPlayer, distance, direction
end

RunService.Heartbeat:Connect(function()
	local nearestPlayer, distance, direction = findNearestPlayer()
	if nearestPlayer then
		if distance <= targetDistance and distance >= stopDistance then
			furnite:Move(direction)
		else
			furnite:Move(Vector3.new())
		end
		
		if distance <= attackDistance and tick() - lastAttack >= attackWait then
			lastAttack = tick()
			nearestPlayer.Character.Humanoid.Health -= damage
		end
	end
end)

Can you not just check in the if statements if the user is the nearest player?

image

And create a if statement check in the kill area to check if the player is the owner or not.

I do not know what your talking about since im new. can you try to say it easier? im also from a different language so its kind of hard to understand.

Not really sure how else to explain it. Just check if the user who owns the NPC is not the one it is following or if it touches it does not kill the player via a if statement check.

for example like

if not nearestPlayer.userID = ---id--- then
return nil```

Well something like this would work:

But on anything were it for example wants the NPC to find and follow the nearest player or kill the player. Not sure if the userIDforOwner is the owner ID but it’s the same principle just replace it with the NPC owner ID.

Basically what it is doing is just checking something else which is if the nearest player ID is not the same then the owner ID. Really just a simple check like that on anything that needs to see if it is the owner will do.

1 Like

Workspace.Workspace.Ape.Ape.Furnite.EnemyController:26: attempt to index nil with ‘UserId’
i get this error.

Oops my bad what you should be doing is checking the player value from the for i, v in pairs() loop rather then the nearestPlayer as it is set to nil.

do you know how to do that?
as said im new lol, i will learn from it

Check the image I showed. It is just the player value u got in the loop which loops the players in the server.

Try this:

Fix
function findNearestPlayer()
	local playerList = Players:GetPlayers()

	local nearestPlayer = nil
	local distance = nil
	local direction = nil
	local userIDforOwner = 13140269348

	for _, player in pairs(playerList) do
		if player.UserId == userIDforOwner then
			continue
		end
		local character = player.Character
		if character then
			local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
			if not nearestPlayer then
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit
			elseif distanceVector.Magnitude < distance then
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit
			end
		end
	end
	return nearestPlayer, distance, direction
end

If player’s user id matches user id of owner, it will skip the player with continue
Also your user id for owner should be a number (remove “”), because player.UserId is a number and you can’t compare string & number

1 Like

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