Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to get this NOT to kill other NPCs?

This is a AI script, but it attacks other AIs. I was wondering if there was a way to prevent this.

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
local torso = script.Parent:FindFirstChild("Torso")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 1000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and (script.Parent.Zombie.Health > 0)then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

function Hit(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human ~= nil then
		human.Health =  human.Health -0
	end
end

larm.Touched:connect(Hit)
rarm.Touched:connect(Hit)
torso.Touched:connect(Hit)

while true do
	wait(0.01)
	local target = findNearestTorso(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Zombie.Sit = false
		script.Parent.Zombie:MoveTo(target.Position, target)
		script.Parent.Head.Moan: Play()
	end
end

2 answers

Check if you can find the name of the Target in game.Players.

If you find it, it's a player, otherwise, it's an NPC.

So, something like this?

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 1000
    local temp = nil
    local temp2 = nil
    local player = nil
    for x = 1, #list do
        temp2 = list[x]
        if (player.className == "Model") and (player ~= script.Parent) then
            temp =  player:findFirstChild("Torso")
            human = player:findFirstChild("Humanoid")
            if (player ~= nil) and (human ~= nil) and (human.Health > 0) and (script.Parent.Zombie.Health > 0)then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

This discussion is preserved as it was. New answers, comments, and votes are closed.