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

How can i make an NPC attack when the player enters and area? [closed]

Asked by 5 years ago

I have tried using multiple scripts making a dinosaur attack a player when the person gets close to the dino. I have seen many scripts but this one seem like it should work but I cant find an error except fot -attempt to index global 'script' (a nil value)- which is a super common error. I think I may need to change some things in the script to apply it to my dino. Dino Models name is LavaRaptor and has Left Arm and Right Arm. Its also playing an idle animation at all times.

The script inside the dino model.

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

Any help would be great. Thank you

0
What line is the error on? OBenjOne 190 — 5y
0
Don't come on this site with free model code, expecting for it to be fixed. User#19524 175 — 5y

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?