Need a little help with while true do and math.random

I need a little help with this. I’m trying to make events for my game whenever it’s night time. I’m using print so I don’t waste a lot of time. When I run it, it would print all of the functions I have added. What I want is to only have one random event each night and not all events in one night. I’m still learning on how to code in Roblox Studio and I also do use the DevHub but didn’t help me that much.

-- The Script
while true do
	wait(5)
	if game.Lighting:GetMinutesAfterMidnight() > 6.25 * 60 and game.Lighting:GetMinutesAfterMidnight() < 17.70 * 60 then -- Morning Time
		print("It's morning time!")
		pmkMoon.Value = false
		bloodMoon.Value = false
		shortVision.Value = false
		darkness.Value = false
		nothing.Value = false
		
		day1_color:Play()
		day2_brightness:Play()
		day3_exposure:Play()
		
		clouds1:Play()
		clouds2:Play()
		
		vision1:Play()
		vision2:Play()
		
		water:Play()
	end
	
	
	
if game.Lighting:GetMinutesAfterMidnight() > 17.75 * 60 and game.Lighting:GetMinutesAfterMidnight() > 6.20 * 60 then -- Night Time
	print("It's night time!")
		local function Pumpkin_Moon()
			print("Pumpkin Moon is rising..")
		end
		local function Blood_Moon()
			print("Blood Moon is rising..")
		end
		local function Short_Vision()
			print("Your vision appears to be getting a lot worse than before..")
		end
		local function Darkness()
			print("It's getting darker than before..")
		end
		local function Nothing()
			print("It appears to be a normal night..")
		end
		
		
		
		local eventsChance = math.random(1, 5)
		if eventsChance == 1 then
			Pumpkin_Moon()
			
		end
		if eventsChance == 2 then
			Blood_Moon()
				
		end
		if eventsChance == 3 then
			Short_Vision()

		end
		if eventsChance == 4 then
			Darkness()

		end
		if eventsChance == 5 then
			Nothing()

		end
	end
end```
1 Like
local db = true

while true do wait(5)
 if db == true then db = false

	if game.Lighting:GetMinutesAfterMidnight() > 6.25 * 60 and game.Lighting:GetMinutesAfterMidnight() < 17.70 * 60 then -- Morning Time
		print("It's morning time!")
		pmkMoon.Value = false
		bloodMoon.Value = false
		shortVision.Value = false
		darkness.Value = false
		nothing.Value = false
		
		day1_color:Play()
		day2_brightness:Play()
		day3_exposure:Play()
		
		clouds1:Play()
		clouds2:Play()
		
		vision1:Play()
		vision2:Play()
		
		water:Play()
	end
	
	
	
if game.Lighting:GetMinutesAfterMidnight() > 17.75 * 60 and game.Lighting:GetMinutesAfterMidnight() > 6.20 * 60 then -- Night Time
	print("It's night time!")
		local function Pumpkin_Moon()
			print("Pumpkin Moon is rising..")
		end
		local function Blood_Moon()
			print("Blood Moon is rising..")
		end
		local function Short_Vision()
			print("Your vision appears to be getting a lot worse than before..")
		end
		local function Darkness()
			print("It's getting darker than before..")
		end
		local function Nothing()
			print("It appears to be a normal night..")
		end
		
		
		
		local eventsChance = math.random(1, 5)
		if eventsChance == 1 then
			Pumpkin_Moon()
			
		end
		if eventsChance == 2 then
			Blood_Moon()
				
		end
		if eventsChance == 3 then
			Short_Vision()

		end
		if eventsChance == 4 then
			Darkness()

		end
		if eventsChance == 5 then
			Nothing()

		end
	end
end
db == true
end

you should format your code so others can easily see it

EDIT: why are you using a debounce in a loop, makes no sense

if the script did not work try use game.Lighting.Changed:Connect()

your here to find the issue or just talking

am I not allowed to inform you about bad code practices?

besides I can’t really type code rn, busy

you have time to inform and dont have time to type a code nice idid not know

two sentences are short
anyways I gtg

Am I supposed to use game.Lighting.Changed:Connect() or somewhere else because it didn’t work.

game.Lighting.LightingChanged:Connect(function() -- // Here?? \\
if game.Lighting:GetMinutesAfterMidnight() > 17.75 * 60 then -- Night Time
		print("Night time :D")
		local function Pumpkin_Moon()
			print("Pumpkin Moon")
			pmkMoon.Value = true
			dark1_color:Play()
			dark2_brightness:Play()
			dark3_exposure:Play()
			clouds1_normal:Play()
			halloweenST.Playing = true
			halloweenTweenStart:Play()
			local function Blood_Moon()
				print("Blood Moon")
				bloodMoon.Value = true
				blood1_color:Play()
				dark2_brightness:Play()
				dark3_exposure:Play()
				blood_water:Play()
				clouds1_normal:Play()
				local function Short_Vision()
					print("Short Vision")
					shortVision.Value = true
					dark1_color:Play()
					dark2_brightness:Play()
					dark3_exposure:Play()
					clouds1_normal:Play()
					blurTween1:Play()
					blurTween2:Play()
					
					local eventsChance = math.random(1,5)
					if eventsChance == 1 then
						Pumpkin_Moon()
					elseif eventsChance == 2 then
						Blood_Moon()
					elseif eventsChance == 3 then
						Short_Vision()
					end
				end
			end
		end
	end
end)```

Here is how I would improve the code commented, the most notable is state management.

State management will solve this problem:

Becuase while the script checks always all the time, it should only trigger once when changing from Morning to Night, simple enough.

-- The Script

--Utilize functions in order to avoid the while loop from being too lengthy
--Much better organization + It's collapsible, ignore unecessary things
local function changeToMorning()
    pmkMoon.Value = false
    bloodMoon.Value = false
    shortVision.Value = false
    darkness.Value = false
    nothing.Value = false

    day1_color:Play()
    day2_brightness:Play()
    day3_exposure:Play()

    clouds1:Play()
    clouds2:Play()

    vision1:Play()
    vision2:Play()

    water:Play()

end

--Define functions ahead of time, not like it's going to change constants
local function Pumpkin_Moon() print("Pumpkin Moon is rising..") end
local function Blood_Moon() print("Blood Moon is rising..") end
local function Short_Vision()
    print("Your vision appears to be getting a lot worse than before..")
end
local function Darkness() print("It's getting darker than before..") end
local function Nothing() print("It appears to be a normal night..") end

--Use tables to organize the events from 1 to 5 in an array, 1,2,3,4,5
local NIGHT_TIME_EVENTS = {Pumpkin_Moon, Blood_Moon, Short_Vision, Darkness, Nothing}

--Track the state of the day time whether it's morning or night
local state = "Morning"

while true do
    task.wait(5) --the new fancy better wait() that doesn't throttle.
    if game.Lighting:GetMinutesAfterMidnight() > 6.25 * 60 and
        game.Lighting:GetMinutesAfterMidnight() < 17.70 * 60 then -- Morning Time
--Only execute this function once depending on the previous state
        if state == "Night" then
            state = "Morning"
            print("It's Morning time!")
            changeToMorning()
        end
    end

    if game.Lighting:GetMinutesAfterMidnight() > 17.75 * 60 and
        game.Lighting:GetMinutesAfterMidnight() > 6.20 * 60 then -- Night Time
        if state == "Morning" then -- this will prevent the night time events from repeating because once it's switched to night time it'll know it's night time and stop executing below.
            state = "Night"
            print("NightTime")
            local eventsChance = math.random(1, #NIGHT_TIME_EVENTS) --Choose from 1 to 5 which is the number of events in the table
            local randomNightEventFunction = NIGHT_TIME_EVENTS[eventsChance] -- store it in table so it's nicer
            randomNightEventFunction()
        end
    end
end
1 Like

Thanks for the help! I also liked when you added comments to some parts of the script where I can understand it. I’ll try and learn your code so I can understand it more and maybe use it in the future.