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

Why isn't the part changing colors?

Asked by 9 years ago

Well, I tried to fix this problem by adding "wait (1.5)" insted of "then." I am a very, very early scripter.

function
owned = BrickColor.Random()
wait (1.5)
owned = BrickColor.Random()

loop = true 

end 

owned = game.Workspace.owned.BrickColor

0
I tried "local" it solved the problem inside the script. The brick doesn't change colors though... iiCasual 20 — 9y

3 answers

Log in to vote
1
Answered by 9 years ago
  1. Your function is being declared without a name
  2. You are never calling your function.
  3. Setting the variable loop to true will do nothing.
  4. You are creating the variable owned that is equal to the BrickColor of the brick. When you change this variable you are only changing that variable in the script and not the property of the brick.
  5. It is good practice to create most variables local unless you need a global variable.

Method 1: Via recursion.

local Brick = Workspace.Owned
function FunctionName()
    Brick.BrickColor = BrickColor.Random()
    wait(1.5)
    FunctionName() --Repeat the same function again
end
FunctionName() --Execute the code inside the function FunctionName

Method 2: using a while loop.

local Brick = Workspace.owned
while true do
    Brick.BrickColor = BrickColor.Random()
    wait(1.5)
end

The while loop will continue looping the code inside of it until the condition is false. And true will never be false so it will run forever.


Method 3: The wait function actually returns true so we could also do:

local Brick = Workspace.owned
while wait(1.5) do
    Brick.BrickColor = BrickColor.Random()
end
Ad
Log in to vote
1
Answered by
LostPast 253 Moderation Voter
9 years ago

Here are a list of things you forgot to do: You forgot to name the function. You forgot to localize and put the local before the script.

local owned = game.Workspace.owned.BrickColor --Make this a habit. Locals almost always go at the beginning of the script.

function BColorC() -- this is the name of the function
owned = BrickColor.Random()
wait (1.5)
owned = BrickColor.Random()
end 

BColorC() -- this calls for the function to start.

This is your script fixed, but if you want it to loop;

local owned = game.Workspace.owned.BrickColor
while wait(1.5) do
owned = BrickColor.Random()
end

while wait() do (what you want the script to do) end

This is a loop that repeats the step every wait()

wait() by itself holds the script for 1/30 of a second before continuing and wait(1.5) holds the script for 1.5 seconds.

Hope this helps :)

Log in to vote
0
Answered by 9 years ago

Before I begin, please tab your code correctly. :)

There are three problems with your code:

1. You did not set a Name for your Function.

2. You are not changing the BrickColor directly, you are only saving it's Color as a Value for the Variable.

3. What if the Child owner was not existant at the time of execution of the code?

There are solutions to this, however, but, please keep in mind; There is a feature to the Studio, the Output, so please use this to check for errors, alright? :)

To fix this, you must;

1. Give the function a Name.

2. Do not set the Variable to it's BrickColor property directly.

3. Use a chunk that will yield the code until the Child is existant, I recommend the WaitForChild for this operation.

Final result;

local owner = game.Workspace:WaitForChild("owned")

function RandomColour()
    if owner then --Checks if `owner` is existant
        owner.BrickColor = BrickColor.Random() --Changes the BasePart's 'BrickColor' property to a random color
    end
end

RandomColor()

Information left out

1. The if statement is used to check if a Argument (or Condition) is true, equal to another Argument, or is equal (or returns) to true.

2. The WaitForChild method is used to yield a specific code until a Child with the same name as the Argument given to the method is existant, then will stop the yield and let the code execute.

Hope this helped!

0
What was wrong with my Answer? TheeDeathCaster 2368 — 9y
0
Nothing, I didn't thumb you down. I can't thumb you down. I need 5 rep before I do and if people keep thumbing me down for no reason I can't. iiCasual 20 — 9y

Answer this question