Advanced NPC that guards a player around a certain radius

I was wondering how I could approach something like the topic I have created. I am planning on making this very advanced, adding certain commands that a specific user can do such as attacking a specific user, protecting a specific user, wedging on the user etc. I really have no idea how to go about this and I was wondering if anybody could give me some help. Any and all help is appreciated, thank you in advance! :smile:

P.S. you can ask questions below and I’d be happy to answer them.

1 Like

For protecting a certain user from other players you could do a loop and make it detect players by using magnitude which could look something like this

local function getPlayerInRadius(player) --player would be the owner of the npc
    local radius = 5 --5 anyone within 5 studs
    local playerHrp = player.Character and player.Character.PrimaryPart

    if not playerHrp then
        return nil --no hrp exist so return nil
    end

    for i, targetPlayer in game.Players:GetPlayers() do
        local hrp = targetPlayer.Character and targetPlayer.Character.PrimaryPart

        if hrp then
            local distance = (playerHrp.Position-hrp.Position).Magnitude --gets the distance

            if distance <= radius then -- checks if the distance is within the radius and if it is returns the target player
                return targetPlayer
            end
        end
    end
end

This would return a player within 5 studs if you want it to return multiple players you could use a table and add players to that and return the table

Now for selecting a user to attack/protect, you could put a click detector in all players’ HumanoidRootPart, and when it’s pressed that becomes the target.

For moving the NPC you could use Pathfinding Service

For attacking a user, this depends on whether the NPC would have a weapon or not so I cannot currently help you on this without knowing more
Hope this helps sorry if anything is explained poorly

1 Like

Thank you, I’ll have to look into this, I’m not too familiar with path finding service.