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

How to tell how many objects are in a certain place?

Asked by
soutpansa 120
6 years ago

I want a function to connect when the game finds that there is only 1 object in a certain place. Let's say if there's only 1 block named "Brick" in the workspace. How would I tell a script to do something if there's only 1 of them? I have no idea how to even start a script like this.

Thanks for reading

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

You could use a for loop:

local function howmanyParts(model)
       local nrofparts
       for i, v in pairs(model:GetChildren)) do
              nrofparts = nrofparts + 1
       end
       return nrofparts
end

Or (im not sure if this works though):

local function howmanyParts(model)
       local nrofparts = model:GetChildren()
       return #nrofparts
end

In each example, the model parameter represents the model that contains the number of parts you want to count. So to print the numbers of instances in the workspace you could do print(howmanyParts(game.Workspace)

0
(to check if the number of parts is greater than one you can do ...if howmanyParts(*model containing the parts*) > 1 then... radusavin366 617 — 6y
0
radusavin366, the second script will work, as you want the number of members of a table Leamir 3138 — 6y
Ad

Answer this question