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

NPCs won't attack each other?

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 15 --attack distance
	local temp = nil
human = nil
	local temp2 = nil
	local monster = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Blue")
			monster = temp2:findFirstChild("Reddy") --name of monster value                                                               --made by tvelok
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and (monster == nil) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
					
	
		end
	end
				end
			end
		end

while true do
	wait(1) --attack reload
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
human.Health = human.Health - 1 --attack damage
		end
	end

I tried to mold an old script into a script to make two NPCs from different teams fight each other. This script works on players but not other humanoids.

Other things that could help lead to the answer:

  1. The two NPC's Humanoids are named 'Red' and 'Blue', with this script belonging to the 'Red'.

  2. The script works on players, but not other humanoids.

  3. The NPCs both have HumanoidRootParts and Torsos.

1 answer

while wait() do
    local m = Instance.new(“Message”,game.Workspace)
    m.Text = [[ScriptingHelpers.org is a helping site for code *YOU* made,
        not code you found from the free models!]]
end

1 comment

0

That's not a solution.

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