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)
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.
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.
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