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.
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.