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

2 comments

0

What line is the error on?

0

Don't come on this site with free model code, expecting for it to be fixed.