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

How to have a script that counts Parts based on BrickColor?

Asked by 5 years ago
Edited 5 years ago

So, I have a script for when a Player touched a Part, it changes a Value to Value = Value + 1, therefore, it adds the Value to when it hit's a specific Value count, the game begins a function.

I need a script for whenever the Part glitches (it happens), to count the amount of Parts in the Model that are Green. As in, it needs to check if the Spot is BrickColor.new('Lime green') this is what i've made so far:

court = script.Parent
val = court.Values.Players



while wait() do
    for i,v in pairs(court:GetChildren()) do
        if v.Name == 'Spot' then
            for i = 1, #v do
                if v.BrickColor == BrickColor.new('Lime green') then
                    val.Value = #v
                else 
                    return
                end
            end
        end
    end
end

Basically, if the Part's ('Spot') inside the ('court') Model are Green then it sets the Value to how many Parts are Green.

2 answers

Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
5 years ago

I'm pretty sure this would work. You were using the 'Brickcolor.new' module which is for changing the color of it. Also you may be able to efficiently use this module - https://developer.roblox.com/api-reference/function/Instance/GetPropertyChangedSignal

court = script.Parent
val = court.Values.Players


while wait() do
    for i,v in pairs(court:GetChildren()) do
        if v.Name == 'Spot' then
            for i = 1, #v do
                if v.BrickColor.Value == 'Lime green'
                    val.Value = #v
                else 
                    return
                end
            end
        end
    end
end
0
BrickColor.Value? im checking the BrickColor property of a part, not of a value. Resplendid 3 — 5y
Ad
Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Never use global variables unless you have to; they're slower and un-needed in your case. Global variables are those without local in front of them. They have to do with scope - lua.org : scope - roblox and where you can use them in your script.

Utilize the WaitForChild() method. Objects replicate out of order, something may be nil and cause your script to break.

Break up your code into functions when you can. It makes your code much easier to read, write, debug, scale, and document.

local court = script.Parent
local values = court:WaitForChild("Values")
local players = values:WaitForChild("Players")

local function countgreenpartsin(parentobject)
    local count = 0 
    for i,v in pairs(parentobject:GetChildren()) do 
        if v:IsA("BasePart") and v.BrickColor == BrickColor.new('Lime green') then 
            count = count + 1
        end 
    end
    return count
end

while wait() do
    for i,v in pairs(values:GetChildren()) do
        if v.Name == 'Spot' then
            players.Value = countgreenpartsin(v)
        end
    end
end
0
ill make sure to try this, but thank you for the help! Resplendid 3 — 5y

Answer this question