I am currently working on an NPC script. But I cannot figure out how to make the NPC attack when in range of a player. Help. I want to make maybe a punch happen, or a projectile or something like that. When the NPC is in range of the player. Heres my script:

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")
local players = game:GetService("Players");

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




while true do
    wait(math.random(1,5))
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end

end

1 comment

0

HELP