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

AI script gone wrong?

Asked by 9 years ago

I am making a zombie AI but it seems that when it kills me it no longer chases anyone. Please help as I can not figure it out.

Here is the script:

torso = script.Parent.Torso
range = 50
armDmg=5
torDmg=10
target = nil

function findNearestTarget()
    plrs = game.Players:GetChildren()
    for i,plr in ipairs (plrs) do
        if plr.Character ~= nil then
            if plr.Character:findFirstChild("Humanoid") and plr.Character.Humanoid.Health>0 then
           tor = plr.Character.Torso
            if target ~= nil then
             if(torso.Position-tor.Position).magnitude < (torso.Position-target.Torso.Position).magnitude then
                print(plr.Name.." is in range")
                target = plr.Character
                break
            end
        elseif (torso.Position-tor.Position).magnitude <= range then
                print(plr.Name.." is in range")
                target = plr.Character
                break
                end
            end
        end
    end
end

function hitArm(hit)
    if hit.Parent ~= nil and hit ~=nil then
        if hit.Parent:findFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage(armDmg)
        end
    end
end
function hitTorso (hit)
if hit.Parent ~= nil and hit ~=nil then
        if hit.Parent:findFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage(torDmg)
        end
    end
end


script.Parent["Left Arm"].Touched:connect(hitArm)
script.Parent["Right Arm"].Touched:connect(hitArm)
script.Parent["Torso"].Touched:connect(hitTorso)    

while true do
    wait (.1)
    findNearestTarget()
    if target ~= nil then
        script.Parent.Zombie:MoveTo(target.Torso.Position,target.Torso)
    end
end

1 answer

Log in to vote
0
Answered by 9 years ago

Well this script does work and it does chase people after it has killed you, I tested it using your script, if you place another scripted humanoid then the zombie will automatically chase it, if you want the zombie to chase people as quickly as possible then increase the range the zombie can follow you (maybe 80 or 100), I noticed the distance you put was quite low. Please accept the answer if it helped, if it didn't then post a comment explaining why.

0
I have just gone back to it and put down a second humanoid and nothing has changed. However I have seen that once I have died the only way it will chase me is if I touch it but apart from that don't know. If somebody could pick out the fault that would help me a lot. lordofpoo 22 — 9y
0
That is if it touches my Torso. lordofpoo 22 — 9y
Ad

Answer this question