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

How do I adapt this NPC chase script to activate only when a player is in range? [closed]

Asked by 6 years ago
Edited 6 years ago

Again, I've searched online for this and tried various ways, for example changing the Dist = 10000 to minDist = (whatever number) and also BestDist = (whatever number). I don't know if that was the right thing to do, sorry, Iam a total noob at lua language. Basically, I want the NPC to follow the player when the player reaches a certain distance from it. Please also tell me if there is a need for a different script or something, Thanks a lot. This is my chase script, what it does is, wherever I place the NPCs, they search the whole map for a player and follow him, e.g. from the other end of the map to the other, which is not what I want. -

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

end

0
Stolen script! Please make your script!! XDDDDDDDDDDDDDDDDDDDD Makz69 0 — 6y
0
Well hey atleast he atleast tried something unlike you who wanted someone to edit the script and give it to you. Also i have a suspicion that your account is a compromised account (unless of course you're trolling and acting like an immature child) GamingOverlord756 48 — 6y
0
Also @YouEnglish do you mean you want the NPC stand still and then chase the player when a player is near it? GamingOverlord756 48 — 6y
0
Thanks @GamingOverlord756 . It is a script I tried very hard to find - as you can see from my previous question a few months ago. I really need this to move on in making the game though. YouEnglish -5 — 6y
View all comments (5 more)
0
And Yes @GamingOverlord756 - Just like the NPCs in the game Ro-Ghoul (for example) YouEnglish -5 — 6y
0
Is the script located inside the player GamingOverlord756 48 — 6y
0
No its inside the NPC - Check out my game - its in the chat YouEnglish -5 — 6y
0
okay i did some googling. Try removing dist and change the line from magnitude < dist to magnitude == (how many studs you want) GamingOverlord756 48 — 6y
0
can you put in the source please i am not understanding - i mean the pure line of code - won't reply for a while soz keep checking later on thx YouEnglish -5 — 6y

Closed as Not Constructive by User#19524

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?