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

How do I make NPCS defend you from hostile mobs?

Hello! I am wondering If there was a way to make NPCS defend you from hostile mobs. This Is good for making games that have npcs protect you like that clone simulator game (Gave me good times) Got anything?

2 answers

✓ Accepted answer

Hello, you can use something like this:


local goodMob = goodmobLocation -- past here defending mob location

local badMob = badMobLocation -- past here bad mob location

local players = game:GetService("Players")


while wait() do -- infinite checking loop
    for i, v in pairs(players:GetChildren()) do
        local char = v:WaitForChild("Character")
        local hrp = char:WaitForChild("HumanoidRootPart")

        if (badMob.Position - hrp.Position).magnitude < 100 then  -- if badMob is less then 100 stoods away from player
            goodMob.Humanoid:MoveTo(badMob) -- this may be wrong, I dont have a lot of experience with MoveTo, you can use pathfinding service
        end
    end
end

2 comments

0

I am relatively new to scripting, please mind that

0

Never mind, It didn't work do u got any Ideas that It may work? (I unanchor It but It didn't work maybe Its the npc attack script I dunno)

Here are some sources that you may find useful:

PathfindingService This can be used to help locate the hostile mobs. Locate their positions and chase after them.

Remember to equip your NPCs with weapons!

Magnitude This can be used to locate the nearest mob.

I've never tried clone simulator, but I think you clone the NPCs to help defend you? if so, check out Clone

If you want your NPCs to have animations, check out How to make Animations in Roblox Studio!

1 comment

0

Thanks man!, I will try thanks I'll tell you If It didn't work or not

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