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

How to make an NPC follow/attack players? (EDITED)

Asked by 6 years ago
Edited 6 years ago

Hi, my previous question was inaccurate so here is the accurate one. My dinosaur NPC DOES follow other models with humanoids but not players with humanoids. If I make a brick and insert a humanoid into that brick then the Dinosaur moves to that humanoid but whenever a player goes near it the dinosaur just ignores the player. So it will follow other parts and models with humanoids but not players. Does anyone know how to fix this and make it the other way around (follows players but not models)? Or if you can't make it ignore models then can anyone at least tell me how to make it follow players too? Here is the script, comment below if you have any questions before you can answer: NOTE: Rex is the name of the dinosaur's Humanoid

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


while wait() do function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 1000
    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

function Hit(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")
    if human ~= nil then
        human.Health =  human.Health -10
        wait(0.001)
        script.Parent.Head.Sound5:Stop()
        script.Parent.Head.Sound6:Stop()
        script.Parent.Head.Sound9:Stop()
        script.Parent.Head.Sound10:Stop()
        script.Parent.Head.Sound8:Play()
        script.Parent.Head.Sound2:Play()
        script.Parent.Rex.WalkSpeed = 0
        script.Parent.Torso.Neck.DesiredAngle = 0
        script.Parent.Head.JawJoint.DesiredAngle = 0
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.1
        script.Parent.Head.JawJoint.DesiredAngle = 0.1
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.2
        script.Parent.Head.JawJoint.DesiredAngle = 0.2
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.3
        script.Parent.Head.JawJoint.DesiredAngle = 0.3
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.4
        script.Parent.Head.JawJoint.DesiredAngle = 0.4
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.5
        script.Parent.Head.JawJoint.DesiredAngle = 0.5
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.7
        script.Parent.Head.JawJoint.DesiredAngle = 0.7
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.9
        script.Parent.Head.JawJoint.DesiredAngle = 0.9
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 1
        script.Parent.Head.JawJoint.DesiredAngle = 1
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.9
        script.Parent.Head.JawJoint.DesiredAngle = 0.9
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.7
        script.Parent.Head.JawJoint.DesiredAngle = 0.7
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.5
        script.Parent.Head.JawJoint.DesiredAngle = 0.5
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.4
        script.Parent.Head.JawJoint.DesiredAngle = 0.4
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.3
        script.Parent.Head.JawJoint.DesiredAngle = 0.3
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.2
        script.Parent.Head.JawJoint.DesiredAngle = 0.2
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0.1
        script.Parent.Head.JawJoint.DesiredAngle = 0.1
        wait(0.001)
        script.Parent.Torso.Neck.DesiredAngle = 0
        script.Parent.Head.JawJoint.DesiredAngle = 0
        script.Parent.Head.Sound7:Play()
        wait(0.7)
        script.Parent.Rex.WalkSpeed = 40
    end
end

larm.Touched:connect(Hit)
rarm.Touched:connect(Hit)
mouth.Touched:connect(Hit)



while true do
    wait(0.1)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Rex:MoveTo(target.Position, target)
    end
end
end

0
For future reference, please use the "For Loop", to loop from [Line 41 - Line 90]. It is better this way as it is less code, better smoothness, and efficient. Also wait(0.001) is almost equivalent to just wait() Axceed_Xlr 380 — 6y
0
Ok, thanks for that. But do you know how I could fix my main problem? Skyraider5 -21 — 6y
0
you have two ends in while true do? What are you ending at line 110? Also use GetChildren() Axceed_Xlr 380 — 6y
0
I think you need .Name on (temp2 ~= script.Parent) then. Axceed_Xlr 380 — 6y
View all comments (4 more)
0
Nevermind I fixed it, Thanks for your help Skyraider5 -21 — 6y
0
What was the error? I would like to know. Axceed_Xlr 380 — 6y
0
Oh so what fixed it was I had to change the "Torso" in line 17 to "RightUpperArm" so that it would recognize that part in every player and I had to change the names of the humanoids in the NPC to NPCHumanoid so that the dinosaur would not recognize the part in them since it only looks for pieces named "Humanoid" Skyraider5 -21 — 6y
0
Sorry, I meant "Torso" in line 16 not line 17 Skyraider5 -21 — 6y

Answer this question