Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I change the time the block changes to be random in between a certain value?

I need to change this blocks appearance in between 18:00 (6:00 PM) and 6:00 AM. I have the the entire script already there practically.

b = script.Parent

local oh,om = 6,00	-- Open Time (hours,minutes)
local ch,cm = 18,00	-- Close Time (hours, minutes)

local l = game:service("Lighting")
if (om == nil) then om = 0 end
if (cm == nil) then cm = 0 end


function TimeChanged()
	local ot = (oh + (om/60)) * 60
	local ct = (ch + (cm/60)) * 60
	if (ot < ct) then
		if (l:GetMinutesAfterMidnight() >= ot) and (l:GetMinutesAfterMidnight() <= ct) then
		b.Material = "Plastic"
		b.BrickColor = BrickColor.new("Black")
		b.Transparency = 0
		else
		b.Material = "Neon"
		b.BrickColor = BrickColor.new("Gold")
		b.Transparency = 0.55
		end
	elseif (ot > ct) then
		if (l:GetMinutesAfterMidnight() >= ot) or (l:GetMinutesAfterMidnight() <= ct) then
		b.Material = "Plastic"
		b.BrickColor = BrickColor.new("Black")
		b.Transparency = 0
		else
		b.Material = "Neon"
		b.BrickColor = BrickColor.new("Gold")
		b.Transparency = 0.55
		end
	end
end

TimeChanged()
game.Lighting.Changed:connect(function(property)
			if (property == "TimeOfDay") then
				TimeChanged()
			end
		end)

Day/Night

-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 12

local cycleTime = dayLength*60
local minutesInADay = 24*60

local lighting = game:GetService("Lighting")

local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime

local timeRatio = minutesInADay / cycleTime

if dayLength == 0 then
	dayLength = 1
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false

Working Product

b = script.Parent

-- New Stuff!!
math.randomseed(tick()%1*1e7)
Thetime = math.random(180,190)

Thetime = Thetime/10
-- Converts Decimal to Minutes
Rounded = math.floor(Thetime)
Rounded = Thetime-Rounded 
Rounded = Rounded * 60
Rounded = math.floor(Rounded)
Thetime = math.floor(Thetime)
-- Final Product
Hours = Thetime
Minutes = Rounded

print("Time has been selected for dark, Hours:" .. Hours .. " Minutes:" .. Minutes)

------------------------------------------------------------------------------------
-- New Stuff!!
math.randomseed(tick()%1*1e7)
Thetime2 = math.random(60,70)

Thetime2 = Thetime2/10
-- Converts Decimal to Minutes
Rounded2 = math.floor(Thetime2)
Rounded2 = Thetime2-Rounded2 
Rounded2 = Rounded2 * 60
Rounded2 = math.floor(Rounded2)
Thetime2 = math.floor(Thetime2)
-- Final Product
Hours2 = Thetime2
Minutes2 = Rounded2

print("Time has been selected for dark, Hours:" .. Hours2 .. " Minutes:" .. Minutes2)



local l = game:service("Lighting")



function TimeChanged()
    local Time = (Hours + (Minutes/60)) * 60
	local Time2 = (Hours2 + (Minutes2/60)) * 60


        if (Time>Time2) then
			if(l:GetMinutesAfterMidnight() <= Time) and (l:GetMinutesAfterMidnight() >= Time2) then
        b.Material = "Plastic"
        b.BrickColor = BrickColor.new("Black")
        b.Transparency = 0
        else
        b.Material = "Neon"
        b.BrickColor = BrickColor.new("Gold")
        b.Transparency = 0.55
        end
		elseif (Time<Time2) then
			if (l:GetMinutesAfterMidnight() >= Time) or (l:GetMinutesAfterMidnight() <= Time2) then
	    b.Material = "Plastic"
        b.BrickColor = BrickColor.new("Black")
        b.Transparency = 0
        else
        b.Material = "Neon"
        b.BrickColor = BrickColor.new("Gold")
        b.Transparency = 0.55
    	    end
		end
	end

TimeChanged()
game.Lighting.Changed:connect(function(property)
            if (property == "TimeOfDay") then
                TimeChanged()
            end
end)

1 comment

0

K, i re-did my answer

1 answer

✓ Accepted answer

Here, I used math.Random to generate a random time,

b = script.Parent

-- New Stuff!!
math.randomseed(tick())


Rand = math.random(1,2)

if Rand == 1 then
Thetime = math.random(180,240)
end
if Rand == 2 then
Thetime = math.random(0,59)
end


Thetime = Thetime/10
-- Converts Decimal to Minutes
Rounded = math.floor(Thetime)
Rounded = Thetime-Rounded 
Rounded = Rounded * 60
Rounded = math.floor(Rounded)
Thetime = math.floor(Thetime)
-- Final Product
Hours = Thetime
Minutes = Rounded

print("Time has been selected, Hours:".. Hours .. " Minutes:".. Minutes)


Rand = math.random(1,2)

if Rand == 1 then
Thetime = math.random(180,240)
end
if Rand == 2 then
Thetime = math.random(0,59)
end

Thetime = Thetime/10
-- Converts Decimal to Minutes
Rounded = math.floor(Thetime)
Rounded = Thetime-Rounded 
Rounded = Rounded * 60
Rounded = math.floor(Rounded)
Thetime = math.floor(Thetime)
-- Final Product
Hours2 = Thetime
Minutes2 = Rounded

print("Time has been selected, Hours:".. Hours2 .. " Minutes:".. Minutes2)

Rand = math.random(1,2)

if Rand == 1 then
Thetime = math.random(180,240)
end
if Rand == 2 then
Thetime = math.random(0,59)
end

Thetime = Thetime/10
-- Converts Decimal to Minutes
Rounded = math.floor(Thetime)
Rounded = Thetime-Rounded 
Rounded = Rounded * 60
Rounded = math.floor(Rounded)
Thetime = math.floor(Thetime)
-- Final Product
Hours3 = Thetime
Minutes3 = Rounded

print("Time has been selected, Hours:".. Hours3 .. " Minutes:".. Minutes3)

local l = game:service("Lighting")


function TimeChanged()
	local mm = l:GetMinutesAfterMidnight()
	print(mm)
    local Time = (Hours + (Minutes/60)) * 60
    local Time2 = (Hours2 + (Minutes2/60)) * 60
    local Time3 = (Hours3 + (Minutes3/60)) * 60

        if (l:GetMinutesAfterMidnight() == Time)  then
              b.Material = "Plastic"
        b.BrickColor = BrickColor.new("Black")
        b.Transparency = 0
        elseif (l:GetMinutesAfterMidnight() == Time2) then
                b.Material = "Plastic"
        b.BrickColor = BrickColor.new("Black")
        b.Transparency = 0
        else if (l:GetMinutesAfterMidnight() == Time3) then
       b.Material = "Plastic"
        b.BrickColor = BrickColor.new("Black")
        b.Transparency = 0
        else if (l:GetMinutesAfterMidnight() < 360 or l:GetMinutesAfterMidnight() > 1080) then
	    b.Material = "Neon"
        b.BrickColor = BrickColor.new("Gold")
        b.Transparency = 0.55
        else
	b.Material = "Plastic"
        b.BrickColor = BrickColor.new("Black")
        b.Transparency = 0
        end
 end
end
end

TimeChanged()
game.Lighting.Changed:connect(function(property)
            if (property == "TimeOfDay") then
print("Yay")
                TimeChanged()
            end
        end)


22 comments

0

I apologize, I should have made it more clear. The script works, I need to make it so the time is random in between 18:00 and 6:00

0

Oh, well then, let me change my answer.

0

The answer has been changed, BTW you should loop this code once every new day starts, unless you want the time the same every day

0

Doesn't seem to be actually changing the block at all, no errors. The function does not seem to be going off.

0

Figured it out, added an end you forgot in the wrong place.

0

oh sorry, well yay!

0

GG apparently it wont work again haha, Ill mess with it and see what I can do.

0

Well, one thing might be if your day-night cycle skips over the time the brick appears. I test it too.

0

It works for me (i set the time to what the output told me)

0

Also I dont know if this is what you did, I am pretty bad at coding in general. I wanted the brick to be black from 6:00:00 to 18:00:00 but like I said random values.

0

And the gold glowing from the times in between, it only sets at that certain time.

0

ok, i'll add more times in between

0

oh, the black is also random?

0

BTW it now has 3 times in between 6 am and pm

0

Let me explain further. So I have these houses and at night the light is supposed to turn on so the neon around 18:00:00-6:00:00, I guess 20:00:00 would be more realistic but what I need is it just at a random time to change to chat then in the morning around 6:00:00-7:00:00 I guess it changes back to black. It works in the original script and I think it would require some <= and >=

0

oh LOL i did 6 am to 6pm, not 6pm to 6am (night)

0

All good man, I'm trying to mess with it a bit more haha

0

Alright, I fixed it but one last problem, the number seems to always generate the same!

0

I did it, finnally

0

oh about that, the reason is beacuse the "seed" used to generate it is the current time. IIf you try again later, you get diffrent times.

0

I know you figured it out your self, but just in case, i have updated my answer with a version that does exactly what you specified in your detailed comment

0

I actually got it to work the way I had originally wanted! I'll post the solution at the end of my post. (I used the original version I had to make it work)

This discussion is preserved as it was. New answers, comments, and votes are closed.