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

for a = 1, 70 help?

Asked by 9 years ago

I am trying to make this change the BackgroundColor3 to one shade less than the current one, can anyone tell me how to make this script work properly?

Code:

local this = script.Parent

while wait(0.03) do
    for a = 1, 70 do
        this.BackgroundColor3 = Color3.new(this.BackgroundColor3-1)
        wait(0.03)
    end
end

Output:

attempt to perform arithmetic on field 'BackgroundColor3' (a userdata value)

2 answers

Log in to vote
0
Answered by 9 years ago
local this = script.Parent

--

for a = 1, 70 do

    this.BackgroundColor3 = Color3.new(

        this.BackgroundColor3.r - (1/255),

        this.BackgroundColor3.g - (1/255), 

        this.BackgroundColor3.b - (1/255)

    )

    wait(1/30)

end

This will run the for loop once, which will reduce the GUI's BackgroundColor3 by Color3.new(70/255, 70/255, 70/255) total, over the course of ~2.3 seconds. I'm not sure exactly what you need this for, so that's the best I could do.

The main problem in your original script was the statement

this.BackgroundColor3 = Color3.new(this.BackgroundColor3-1)

Color3.new(r, g, b) takes 3 parameters ranging from 0 - 1. Also, you cannot subtract an integer (1) from a userdata value (Color3).

0
Thanks. ;D ISellCows 2 — 9y
Ad
Log in to vote
0
Answered by
funyun 958 Moderation Voter
9 years ago

Color3 is kinda like Vector3. You can't subtract a number from the set itself...

this.BackgroundColor3 = Color3.new(this.BackgroundColor3-1)

but you can subtract one from each element in the set...

original = this.BackgroundColor3
this.BackgroundColor3 = Color3.new(this.r - 1, this.g - 1, this.b - 1) --r, g and b is the equivalent of x, y and z in Vector3.

However, Color3 is a bit weird. In the properties window, the values of Color3 seem to be [255, 255, 255]. However, its true value is [1, 1, 1]. Thus, [0, 0, 0] is pitch black, and [1, 1, 1] is pure white. If we subtract 1 from the Color3 values, we're immediately going from white to black. To fix this, divide by 255:

original = this.BackgroundColor3
this.BackgroundColor3 = Color3.new(original.r - 1/255, original.g - 1/255, original.b - 1/255)

Full script:

local this = script.Parent

while wait() do --The default value for the wait function is set to 0.03.
    for a = 1, 70 do
    local original = this.BackgroundColor3
        this.BackgroundColor3 = Color3.new(original.r - 1/255, original.g - 1/255, original.b - 1/255)
        wait()
    end
end
1
The variable 'this' is referring to a GUI element. On line 6, you're attempting to reference 'this.r', 'this.g', and 'this.b'. I believe you mean 'this.BackgroundColor3.r', 'this.BackgroundColor3.g', etc Chaotic_Cody 154 — 9y

Answer this question