Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Whats wrong with this basic script?

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

✓ Accepted answer

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)

4 comments

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

0

Both Perci1 and BosswalrusTheCoder's answers are problems which would stop this script from working; apply both fixes

0

Didn't see that. Edited it now, thanks boss.

0

Gave both you and Boss an answered, thanks for the help :)

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

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)

1 comment

0

Hm, nope.

This discussion is preserved as it was. New answers, comments, and votes are closed.