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

Why does the brickcolor keep changing after I touch it once and walk away?

So, Im trying to make a script for someone where if you touch it it randomly changes to either red, or green, but whenever I touch it, and don't touch it again it keeps changing. (no errors in the output because its doing what its supposed to.) I hope you can help me!

local part = script.Parent

local function onTouch(hit)
    while true do
	wait(1)
   local e = math.random(1,10)

if e >= 5 then
	  part.BrickColor = BrickColor.Green()
 else
	  part.BrickColor = BrickColor.Red()
       break
    end
  end
end
part.Touched:Connect(onTouch)

The script is inside a part.

2 comments

0

When I say its doing what its supposed to do it isnt actually DOING WHAT ITS SUPPOSED TO DO I just mean that its a "Proper script"

0

Here is a link to how to use a debounce: http://robloxdev.com/articles/Debounce

1 answer

✓ Accepted answer

So your problem is that you have your code inside of a while loop so every second the color will change after you touch the brick. If you want the color to change only when you touch the block then remove the while loop. Hope this helps and have a great day scripting! Edit: When you touch a block you actually end up touching it multiple times so the loop will start multiple different times and the break will only take effect for one of the loops after a random chance number. I would recommend using a debounce.

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