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

AI script gone wrong please help?

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.

But when it touches my torso it begins chasing me again!

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
1
Answered by 9 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Okay, I'll go in depth a bit more.

So, first, let's change our variables to local, so we're not storing unnecessary data in the script's function environment.

-- Let's make some local variables

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

function findNearestTarget()

    -- Let's also use "GetPlayers()" instead of "GetChildren()"
    -- Notice how we make it a local variable.

    local plrs = game.Players:GetPlayers() 

    for i,plr in ipairs (plrs) do
        if plr.Character then
            if plr.Character:findFirstChild("Humanoid") and plr.Character.Humanoid.Health>0 then
                local tor = plr.Character:FindFirstChild("Torso") -- also local
                if target and tor then
                    if (torso.Position-tor.Position).Magnitude <= range then -- compare distance to our "range" variable.
                        print(plr.Name.." is in range")
                        target = plr.Character
                        break
                    end
                end
            end
        end
    end
end

-- Let's not forget to indent these functions as well.

function hitArm(hit)
    if hit.Parent and hit then
        if hit.Parent:findFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage(armDmg)
        end
    end
end

function hitTorso(hit)
    if hit.Parent and hit 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 wait(0.1) do -- You can call functions in a loop condition as well.
    findNearestTarget()
    if target then
        script.Parent.Zombie:MoveTo(target.Torso.Position,target.Torso)
    end
end

Now, there are a few things we should elaborate on.

1: Comparing positions

We see in your code that we have something like:

if (torso.Position-tor.Position).Magnitude < (torso.Position-target.Torso.Position).Magnitude then
    -- ...
end

Now, here, we want to compare the magnitude of these 2 vectors, with the "range" variable. So it would now look something like this:

if (torso.Position-tor.Position).Magnitude < range then
    -- ... target in range
end

Other than that, indenting seems pretty good and you should probably get into the habit of using if statements like:

if Variable then
    -- ect
end

-- Oppose to ...

if Variable ~= nil then
    -- ect
end

Just to make the code seem a bit neater, and eliminate obsolete comparisons.

Hope this helped, let me know if you have any questions.

0
Don't just post code--explain what you did. At the very least you should include comments that highlight the important differences. BlueTaslem 18071 — 9y
0
I guess that's the result of posting without sleep for 2 days. Let's hope this updated post makes it a bit clearer. CodingEvolution 490 — 9y
Ad

Answer this question