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

How do I create a follow script for an Enemy NPC that choose the users around? [closed]

Asked by 5 years ago
Edited 5 years ago

I'm making a RPG game and I don't know how to make an enemy NPC that follow and kill players. If have anyone know that, please help me. I have try to run this script in Roblox Studio but it not do anything. This is my script code I don't know how to fix it:

players = nil; --Where we are going to store all the players
dummy = game.Workspace.Dummy; --Dummy reference
currentPlayerFollowing = nil; --The current player the zombie is following
damage = 2; --Change this for more damage if you want

function touched(hit)
  --pcall allows for errors to be disregarded so you don't have to worry about random output errors
   if pcall(function() if hit.Parent.Humanoid ~= nil then end end) then --If the character's health storage is there
   pcall(function() hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - damage end); --Do damage
   end
end

dummy.Torso.Touched:connect(touched);

while wait(1) do --Forever loop
 players = game.Players:GetChildren(); --Grab all the Players currently in the game

 for i = 0, table.getn(players), 1 do --Go through all of the players
  if (players[i] ~= nil and (players[i].Character.Torso.Position - dummy.Torso.Position.X).magnitude <= 10) then --Subtract the distance between the player and the zombie
   currentPlayerFollowing = players[i]; --Add the player to the currentPlayerFollowing reference so the zombie can follow them
  end
 end

 if (currentPlayerFollowing ~= nil) then --If the player is there
    dummy.Humanoid.WalkToPoint = currentPlayerFollowing.Character.Torso.Position --Walk to the player
 end
end?

What happend with this? Please help me! Thank for your help.

0
You don’t know how to fix it cause it’s not yours. User#19524 175 — 5y

Closed as Not Constructive by User#19524, T0XN, and evaera

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 5 years ago

Try this:

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 = 45
    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) 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(1)
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        script.Parent.Zombie:MoveTo(target.Position, target)
    end

end

local dist = 45 --Change the number to the amount of studs the player has to be for the enemy to start chasing!

I'm assuming the enemy humanoid is named (Zombie) If it isn't go to line 34 and cahge Zombie to whatever the humanoid name is.

Hope this helps!

Ad