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

Script that moves to target named Zombie?

Asked by
bloxxyz 274 Moderation Voter
9 years ago

Okay, so I'm not entirely sure if these questions are allowed, but if they are, feel free to reply. This is not my script. It is one I have found from a model, I wish to edit it so I can learn better and I feel it would really help me if the community could help me with a thing or two about the script I found and some things I've had trouble locating in the wiki.

Thank you so much ( I commented on the things I don't understand, the rest I get ):

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 = 1000
    local temp = nil --Why make a variable for nil?
    local human = nil -- ^^
    local temp2 = nil -- ^^
    for x = 1, #list do
        temp2 = list[x] -- What exactly is the [x] for? Is this restating or..?
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Torso")
            human = temp2:findFirstChild("Zombie")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp -- nil to nil? do not understand
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

Again, thanks. Just a few things I pointed out and hope you can clarify. Cheers :)

1 answer

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
9 years ago
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 = 1000
    local temp = nil --These are made nil because the part that they refer to later in the script isn't in the game at the beggining. These aren't really even needed here, the script should work fine without declaring these variables nil.
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x] -- The x is used to find the part in the table (If you don't understand "for" functions,  look at this page: http://wiki.roblox.com/index.php?title=Generic_for). Anyways, it finds the children in the table that was created on line 5 and changes the variable "temp2" from nil to the child.
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Torso")
            human = temp2:findFirstChild("Zombie")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp --This isn't "nil to nil". If you look on line 14, temp is the character's torso. The script is changing the "torso" variable from nil to the character torso.
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

I edited what you wrote and did my best to explain what I could...If you have any further questions, please leave a comment below. Hope I helped :P

0
Thank you. This makes a lot of sense. bloxxyz 274 — 9y
0
Awesome. Glad to help :P dyler3 1510 — 9y
Ad

Answer this question