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

I do not understand this script and want to know what i=1, #ch is?

Asked by 7 years ago
function onTouch(obj)
    if obj.Parent:findFirstChild("Humanoid")~=nil then--I know this checks for a humanoid
        p=game.Players:findFirstChild(obj.Parent.Name)--This searches the name of the person I believe.
        if p~=nil then -- if there is no name?
            ch=p.Backpack:getChildren() --Checks the children of the backpack.
            for i = 1, #ch do -- I do not understand this.
                if ch[i].Name~="Examine" and ch[i].Name~="Get" and ch[i].Name~="Window" and ch[i].Name~="Mop" and ch[i].Name~="Bathroom" and ch[i].Name~="Water" and ch[i].Name~="Weed" then ch[i]:Remove() end--I do not understand this if function can someone explain it to me?
            end
        end
    end
end
script.Parent.Touched:connect(onTouch)

I do not really comprehend this script, but I need help comprehending it.

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

First of all, if p~=nil then is checking if p actually exists, the ~= part means "does not equal." The next part ch=p.Backpack:getChildren() puts all the children of Backpack into a table. With all the children in a table you can iterate over each entry and check the child for specific properties, names, etc... This is where the "for" function comes in. it's saying that for as long as there is an entry in ch (one of the children) then run the function, and increment "i" (which is a variable initialized as 1) by 1. For every time it encounters another entry in the ch table it's going to check a few things, first it's going to check if the name of the object at table entry "i" does not equal "Examine".

How Tables Work:

Tables are laid out as a bunch of entries one after the other, where each entry is the position (or number) of the last entry plus 1 by default. For example if the ch table had four entries in it, it would look like this: ch{"entry 1", "entry 2", entry 3", entry 4"}. so if we wanted to find what's at entry 3 we would check ch[3]. This is where "i" comes in, since we increment "i" every time we run the function it's always going to check ch at a new entry. Then, for every entry, it runs the if statement checking if the object name does not equal all the various names laid out above, and if it doesn't equal any of them, then remove the object.

0
Just to add a few things: #ch is simply the number of entries in the list, so that we will go through all of them. Also note that many of the practices used in this code are not the best nor most recent ways of doing it, so double check stuff on the wiki or ask about it here if you're not sure what to use. Perci1 4988 — 7y
Ad

Answer this question