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

Doesn't 'v' Represent the children here?

Asked by
Chronomad 180
8 years ago

I have this script that should get the children of a GUI Framehange their background color based on the value BrickColor (Or their name!) However, for some reason it tries to change the properties of the Frame. How can I get it to change the color of the textbuttons inside instead? Thanks for any help

local G = script.Parent

for _,v in pairs(G:GetChildren()) do
    local brickcolor = BrickColor.new(v.BrickColor.Value) -- or v.Name
        local color = brickcolor.Color
        v.BackgroundColor3 = Color3.new(color)
        wait(0.1)
        script:Destroy()
end

Alternatively--

local G = script.Parent

for i = 1, #(G:GetChildren()) do
    local brickcolor = BrickColor.new(G.BrickColor.Value)
        local color = brickcolor.Color
        G.BackgroundColor3 = Color3.new(color)
        wait(0.1)
        script:Destroy()
end

0
destroy the script after/outside the for-loop? XAXA 1569 — 8y
0
It breaks because you destroy the script after setting the color of the first child. Vlatkovski 320 — 8y

1 answer

Log in to vote
0
Answered by
Wutras 294 Moderation Voter
8 years ago
local G = script.Parent

for _,v in pairs(G:GetChildren()) do -- v represents one object in the table returned by :GetChildren()
        local brickcolor = BrickColor.new(v.BrickColor.Value) -- or v.Name
        local color = brickcolor.Color
        v.BackgroundColor3 = Color3.new(color)
        wait(0.1)
        script:Destroy() -- This destroys the script after the first colouring.
end
local G = script.Parent

for _,v in pairs(G:GetChildren()) do
    if v:IsA("TextButton") then -- Checks whether the classname of v is TextButton; that's used to prevent the script from trying to colour a script and breaking by that
    if v:FindFirstChild("BrickColor") then -- Checks whether there is the BrickColor value
        local brickcolor = BrickColor.new(v.BrickColor.Value)
        local color = brickcolor.Color
        v.BackgroundColor3 = color -- the color variable should be enough already.
    elseif BrickColor.new(v.Name) then -- Checks whether the name would make a BrickColor
        local brickcolor = BrickColor.new(v.Name)
            local color = brickcolor.Color
            v.BackgroundColor3 = color -- the color variable should be enough already.
    end
        wait(0.1) -- You could just remove this to make it seem like it all changes the colour in the same moment.
end
script:Destroy() -- Now that the :Destroy() is after the loop it will destroy the script afterwards and not during the coloruing.

If you have any other questions, feel free to ask!

Ad

Answer this question