I need help with my chance event

Hello everyone, its me again. :smiley:

So yesterday I created a flickering lightbulb with code. (Exiting, I know)

Today I wanted to spice things up so I added an “event”(You could say)

The lightbulb will flash red, it has a 1/2 percent chance to flash red.

Here’s the code I used.

Herse my output:rbWUjdbqrQ

Now, here’s the actual product:
TTgZoS5GVu

I think it worked pretty well, right? :thinking:

Wrong

I tried switching the numbers, instead of 1,2 I used 1,100 and this happened

Herse my code:

And here’s my output
y0AlcmJ3ch

Now, here’s the product:
aRvcsUotOM
Now, looking at the output I can tell something is clearly wrong. But looking at the code I don’t see anything wrong about it.

Can anyone please explain to me why it isn’t working? Maybe I should remove something or change something?

1 Like

try this:


while wait() do
   local randomNumber = math.random(1,2) 
   if randomNumber == 1 then
      -- do something
   elseif randomNumber == 2 then

      --do something
   end
end
1 Like

yes, clearly, there’s something wrong.
You only setted, if randomNumber == 1 or 2, not from 1, to 1K, which I don’t think you want to do, there’s plenty way to make this better and more efficient

And also, you’re puttin a while loop

inside a while loop

FIRST ERROR Looking at your script, your random number only generates once meaning that it will have the same random number for every time the while loop ran the code. To counter this, you have to make the random inside of the while loop.

SECOND ERROR After re-reading your code I also found that once the code went into the second while loop of changing the light bulbs the code would get stuck in that code forever. Meaning, it would repeat this code and wouldn’t run anything else until it broke free from the loop. To counter this either delete the while loop and use a for loop to run the light pattern a few times or add a break once the pattern is done.

THIRD ERROR I think you made a mistake because on line 48 there is an end yet it’s not really ending anything. This will error your code. You probably forgot to add the while true do.

Here is some example code:

while wait(1) do
	local RandomNumber = math.random(1,2)
	if RandomNumber == 1 then
		print("First One")
	elseif RandomNumber == 2 then
		print("Second One")
	end
end

If you were to read the output it would put something similar to this:

First One (x2)
Second One
First One
Second One
First One (x3)
Second One (x2)

(You get the point).

So, in your code, instead of putting print("First One") or print("Second One") you would put your changing lightbulb script.

To Sum It All Up

Your script only generated a random number once meaning, in your while loop it would repeat the same light bulb pattern every time. So as I’ve said previously, you need to make the random number in the while loop so that way it generates a new number every time a new light pattern is needed.

Also, instead of having while loops inside the pattern you would use a for loop to run the pattern your desired amount of times.

Your code would probably look something like this:

-- Put your lighting variables here

while true do
	local RandomNumber = math.random(1,2)
	if RandomNumber == 1 then
        -- this loop would run the code howmanytimes you want
        for numberoftime = 1, HOWmanyTIMES, 1 do
           -- Your lightbulb pattern here
        end
	elseif RandomNumber == 2 then
		 -- this loop would run the code howevertimes you want
        for numberoftime = 1, HOWmanyTIMES, 1 do
           -- Your lightbulb pattern here
        end
	end
end
1 Like

Thanks for the feedback and help!

1 Like