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

Locking an NPC in a designated area and restricting them from directly touching the player?

Asked by
Donut792 216 Moderation Voter
4 years ago

alright so you are gettinga 2 in 1 deal here 2 questions for the click of 1 the first question is how would i keep a roaming npc in a designated area that is set by only 1 part (im assuming some way with part:TouchEnded? or if that doesnt work something with collision groups but i tried messing with collistion groups and it never worked out the best i got was the character being able to phase through any surface except the top and)

and the bonus question is how would i be able to get an NPC to not touch the player then stop and play use its tool in the direction of the player basically i want it to approach you and swing a sword at you which i can get the sword swinging done but im oblivous on how to get them to just stop a certain amount of studs from you without using directly x or z cordinates (im thinking go off of player lookvector somehow???) but here is what i already have set up it is very basic because i found it in someone elses NPC in the toolbox

Script:

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 85
    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("HumanoidRootPart")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and game.Players:FindFirstChild(temp2.Name) 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

i was thinking on script.Parent.Humanoid:MoveTo(target.Position, target) i could change it the parameters to (target.Position - (idk what to put here), target) but yeah i dont think x or z cordinate could do it without them wondering off to your side before turning at you which i also dont know how to get them to turn and face the player but im assuming it also has something to do with lookvector

Answer this question