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

Whats wrong with this basic script?

Asked by 9 years ago

I have tried to make a basic script that when you touch it, it turns the brick to a random color, makes it half transparent, then in 2 seconds close and change to a random color again.

local Door = script.Parent

function onTouch()
    Door.CanCollide = false
    Door.Transparency = 0.5
    Door.BrickColor = BrickColor:Random()
    wait(2)
    Door.CanCollide = true
    Door.Transparency = 0
    Door.BrickColor = BrickColor:Random()

end
Door.Touched:connect(OnTouch)

3 answers

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

You're main problem is here: BrickColor:Random()

However, it should be BrickColor.Random()

Also, you will most likely need a debounce. A debounce is basically a variable that prevents your code from running until it's finished the first time.

You're final code should look like this:

local Door = script.Parent
local debounce = false

function onTouch()
    if debounce == false then
        debounce = true --Stops your code from running until it equals false again.
        Door.CanCollide = false
        Door.Transparency = 0.5
        Door.BrickColor = BrickColor.Random()
        wait(2)
        Door.CanCollide = true
        Door.Transparency = 0
        Door.BrickColor = BrickColor.Random()

        debounce = false --Let's your code run again
    end
end
Door.Touched:connect(onTouch)

0
I tried it, it says this in the console when I touch it 12:26:06.284 - attempt to call a nil value 12:26:06.285 - Disconnected event because of exception ggggyourface23 63 — 9y
0
Both Perci1 and BosswalrusTheCoder's answers are problems which would stop this script from working; apply both fixes BlueTaslem 18071 — 9y
0
Didn't see that. Edited it now, thanks boss. Perci1 4988 — 9y
0
Gave both you and Boss an answered, thanks for the help :) ggggyourface23 63 — 9y
Ad
Log in to vote
4
Answered by 9 years ago

Maybe because you are connecting (OnTouch) and not (onTouch)

Log in to vote
-1
Answered by 9 years ago

I'm not sure if this will work, but I think this is the correct way to set a brick's color randomly: Door.BrickColor = BrickColor.new(math.randon())

also, if you want it to infinitely change color, then you need a while loop:

Door.Touched:connect(function()
    while wait(2) do
        Door.BrickColor = BrickColor.new(math.random())
    end
end)
0
Hm, nope. ggggyourface23 63 — 9y

Answer this question