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

How do I make a NPC move around randomly?

Asked by 6 years ago

Hi, I am making a game, a survival game, where the players are in a Jurassic Park-like dinosaur theme park on an island. There are lots of cages/exhibits that hold NPC (non-player characters) dinosaurs that end up escaping their exhibits and the players have to survive for five minutes without the dinosaurs killing them (by touching/biting them, for most dinosaurs it takes multiple hits to kill the player but for one it only takes one hit). The problem is, I want the dinosaurs, after escaping their cages, to walk around the island until they find a player. Once they find a player within a certain range they can chase the said player and try to kill them. The following part is fine, I have got that working but the issue is with the walking around the island part. I have looked on Roblox and the Roblox wiki and read about NPC movement but all I could find is how to make it walk from one point to another or on a pathway from one point to another. If worst comes to worst I can maybe deal with making the dinosaurs travel on a pathway across various areas on the island but I am worried that if I do that then the players might realize the dinosaurs are following a path and then they will easily be able to avoid those paths. Because of this I REALLY need the dinosaurs to just walk randomly around the island until a player comes within range. Does anybody know a way that I can do this? And if it isn't possible, does anyone know how I can make the dinosaurs travel the island in as random a way as possible?

Thank you so much for your help, Skyraider5

1
Although this is not an answer, usually in scripting helpers you don't give that much detail. Usually you describe what you are trying to do without context. Like; "I need the NPC to follow the closest player at a certain speed." Something simple like that. The context of the situation isn't needed to answer the question. NickDestroyer123 12 — 6y
0
Ok, but that is completely irrelevant an while I appreciate the comment, I would rather reserve the answers and comments to people who are actually trying to help me out with my question and not for people who are criticizing my questions format. thank you Skyraider5 -21 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Put this in the NPC you are tying to function. Make sure that this code is in a regular script.

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 25 -- Change this if you want the NPC to detect you further out.
    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("Torso")
            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(0.1)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
end

This should work. If this worked, please accept this answer.

0
ok thanks, I'll give it a try and see if it works Skyraider5 -21 — 6y
0
im doing something almost the same but it didnt work what should i change form my personal npc JakeDaBeAat 13 — 6y
0
died danglt 185 — 5y
Ad

Answer this question