# Math.random not working?

I am trying to make a timer script based on 3 NumberValues and everything except the TimerValue script is working. I am not getting any errors in the output so I have no idea what I could have done wrong.

``````local MainValue = script.Parent.Parent.MainValue
local TimeToBreakValue = script.Parent.Parent.TimeToBreak
local ActivatedYesOrNoValue = script.Parent.Parent.ActivatedYesOrNo
local ProxPrompt = script.Parent

if ActivatedYesOrNoValue.Value == 2 then

repeat
TimeToBreakValue.Value = TimeToBreakValue.Value - 1
wait(1)
until
TimeToBreakValue.Value == 0

end

if TimeToBreakValue.Value == 0 then

ActivatedYesOrNoValue.Value = 1
MainValue.Value = math.random(1, 2)

end

if TimeToBreakValue.Value >= 0 then

ActivatedYesOrNoValue.Value = 2

end
``````

The script should choose between 1 and 2. Any Ideas why it isnâ€™t working?

1 Like

Iâ€™m very confused on the logic in your script. Why is a YesOrNo Value an int value? Shouldnâ€™t it be a boolean? And why are you subtracting it until itâ€™s zero, and then checking if itâ€™s zero later?

3 Likes

YesOrNo Value is there because I have 2 scripts checking if its true or false. 2 is meant for true and 1 for false. Iâ€™m subtracting 0 and then checking if itâ€™s zero later because the script is supposed to be reusable

Well Iâ€™ll just ignore the logic for now. Iâ€™m pretty sure math.random is working perfectly fine. What makes you think it isnâ€™t?

Well the whole behind this script is that is based on random events. Math Random is there to pick a random number and another script reads that number and aligns it to a scenario based to that number. For example if math random picks 1 then its a simple power outage, if math random picks 2 then the plugs exploded. Its not working because when I tested it with a light, the light did not turn off like in the scenario (every scenario has the light turned off). But I think the problem is with TimeToBreak == 0 because the random number doesnâ€™t get picked.

Iâ€™ve added prints to every â€śif thenâ€ť and everything prints. I have literally no idea what could be wrong.

This probably wonâ€™t fix anything yet, but I simplified the code:

``````local GameMode = script.Parent.Parent.MainValue
local BreakTime = script.Parent.Parent.TimeToBreak
local ActivationState = script.Parent.Parent.ActivatedYesOrNo
local ProxPrompt = script.Parent -- this is unused

if ActivationState.Value == 2 then