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

(Solved)How can I detect npc with the same name with a for loop?

Hi, you can try to detect the npc with the same name with a for loop and what I get is to detect all the objects including the ground and the ground. Is there anyone who would like to help me out with this?

1 answer

wait(2)
local enemy = game.Workspace.Enemy:GetChildren()
local Players = game:GetService("Players"):GetPlayers()	
function getHumanoid()
	local humanoidPos = {}
	for i , player in pairs(Players) do
		local character = game.Workspace:WaitForChild(player.Name)
		for i, child in pairs(enemy) do
			if child:FindFirstChild("Humanoid") and child ~= script.Parent then
				table.insert(humanoidPos, (script.Parent.chasis1.Position - child.HumanoidRootPart.Position).magnitude)
						
				script.Parent.chasis1.Touched:Connect(function(touch)
					local Human = touch.Parent:findFirstChild("Humanoid")
	
					if Human and Human.Parent.Name ~= character.Name then
						Human:TakeDamage(20)
						local Sound = Instance.new("Sound",script.Parent.chasis1)
						Sound.SoundId = 'rbxassetid://743886825'
						Sound:Play()
						wait(0.2)
						Sound:Destroy()
						wait(1.5)
						print(child)
					end
				end)
					
			end
				
		end
		table.sort(humanoidPos)
		for i, child in pairs(humanoidPos)do
			if i ~= 1 then
				table.remove(humanoidPos, i)
			end
				
		end
		for i, child in pairs(enemy)do
			
			if child:FindFirstChild("Humanoid") and child ~= script.Parent then
				if (script.Parent.chasis1.Position - child.HumanoidRootPart.Position).magnitude == humanoidPos[1] then
							
					return child
				end
			end
		end	
	end
end


getHumanoid()

2 comments

0

It already detects the enemies but when I sit in the car it also detects the player. What could I do?

0

Solved

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