Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
brok4d 77
5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by
brok4d 77
5 years ago
Edited 5 years ago
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()

0
It already detects the enemies but when I sit in the car it also detects the player. What could I do? brok4d 77 — 5y
0
Solved brok4d 77 — 5y
Ad

Answer this question