Using a value with a ":"

Hello!

I have made a script which changes the color of terrain depending if it’s night/day. I have made it so if the world time reaches a certain point (7:00:00) for example, it would start the twean script, but the : doesn’t work. When I try to uses “” 's, it says that I attempted to compare a value and number. Any help will be appreciated.

R3M1X

Please post your current script.

I’m assuming you’re checking the TimeOfDay of the Lighting Object? If so, it’s a string, to compare it in an operand you would need to do something similar to:

if LightingObj.TimeOfDay == "14:00:00" then
-- do stuff
end

The script is as follows:

local terrain = game.Workspace.Terrain

local Time = game.Lighting.TimeOfDay

local daynight = script.Parent.DayNight.Value

print(“gotpastcode3”)

local colorValue = Instance.new(“Color3Value”)

colorValue.Value = terrain:GetMaterialColor(“Grass”)

colorValue.Parent = script.Parent

print(“gotpastcode2”)

colorValue:GetPropertyChangedSignal(“Value”):connect(function()

terrain:SetMaterialColor(“Grass”, colorValue.Value)

end)

print(“gotpastcode1”)

local tweenInfo = TweenInfo.new(60, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

local Night = game:GetService(“TweenService”):Create(colorValue, tweenInfo, {Value = Color3.fromRGB(91, 122, 380 )})

local Day = game:GetService(“TweenService”):Create(colorValue, tweenInfo, {Value = Color3.fromRGB(110, 148, 70)})

print(“got past code”)

while true do

TimeOfDay = game.Lighting:GetMinutesAfterMidnight()

print(“Looped”)

if TimeOfDay >= ‘21:00:00’ and daynight == 0 then

Night:Play()

print(“Tweened”)

daynight = 1

end

if TimeOfDay >= ‘6:00:00’ and daynight == 1 and TimeOfDay <= ‘7:00:00’ then

Day:Play()

print(“Went back”)

daynight = 0

end

wait(0.1)

end

It is indented properly in game

So the issue is how you’re comparing the strings, you can’t perform a greater/equal than operand on two strings:

local hours = TimeOfDay:split(':')[1]
if hours > 6 and hours < 7 then
-- time is between 6 and 7
end

You’re trying to get if a string is greater than a number. Use string.split like @Isocortex said or use ClockTime.

Only 1 small problem: it says that it attempted to index number with “split”. What should I do to fix this?

R3M1X

I didn’t actually read your code, just saw immediate issues. You’re actually getting the “MinutesAfterMidnight” property for the variable ‘TimeOfDay’ - either set that to Lighting.TimeOfDay, or do this:

TimeOfDay = string.format("%02d:%02d", TimeOfDay / 60, TimeOfDay % 60)

to get the hours from the minutes after mightnight

1 Like