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

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

Asked by
AltNature 169
5 years ago

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.

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" AltNature 169 — 5y
0
Here is a link to how to use a debounce: http://robloxdev.com/articles/Debounce User#21908 42 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

Ad

Answer this question