String Value not changing

Hey y’all, so today I’ve been working on a day and night system for my game but ran into another issue. When trying to change my time of day, I noticed that the string value holding the information wasn’t changing as intended. I don’t believe this feature was out of my skillset, but now I’m just left confused as to why it isn’t working correctly. Any help figuring this out will be greatly appreciated <3

Server Script:

local WeekDay = game.ReplicatedStorage.WeekDay
local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local globalVariables = require(ReplicatedStorage.Modules.globalVariables)

local dayDuration = 720 -- How many real-time seconds an in-game day will last
local nightDuration = 720 -- How many real-time seconds an in-game night will last

local WeekDay = ReplicatedStorage.WeekDay
local TimeOfDay = ReplicatedStorage.TimeOfDay

Lighting.ClockTime = 6

--Tween
function tween(l, p)
	TweenService:Create(Lighting, TweenInfo.new(l, Enum.EasingStyle.Linear, Enum.EasingDirection.In), p):Play()
end

--Change the day of the week
function changeWeekDay()
	globalVariables.currentWeekDay += 1
	if globalVariables.currentWeekDay > 6 then
		globalVariables.currentWeekDay = 0
	end
	WeekDay.Value = globalVariables.weekDays[globalVariables.currentWeekDay]
	--print(globalVariables.weekDays[globalVariables.currentWeekDay])
end

function changeTimeOfDay()
	globalVariables.currentTime += 1
	if globalVariables.currentTime > 5 then
		globalVariables.currentTime = 0
	end
	TimeOfDay.Value = globalVariables.timesOfDay[globalVariables.currentTime]
	--print(globalVariables.timesOfDay[globalVariables.currentTime])
end

--Tween colors
while type(dayDuration) == "number" and type(nightDuration) == "number" do
	--Change the day of the week and time
	if Lighting.ClockTime == 0 then
		changeWeekDay()
	elseif Lighting.ClockTime == 6 then
		changeTimeOfDay()
	elseif Lighting.ClockTime == 6.5 then
		changeTimeOfDay()
	elseif Lighting.ClockTime == 12 then
		changeTimeOfDay()
	elseif Lighting.ClockTime == 17 then
		changeTimeOfDay()
	elseif Lighting.ClockTime == 17.5 then
		changeTimeOfDay()
	elseif Lighting.ClockTime == 18.2 then
		changeTimeOfDay()
	end

	-- Change colors only with 'LightingFix' enabled
	tween(4, { OutdoorAmbient = Color3.fromRGB(60, 60, 60), FogColor = Color3.fromRGB(25, 25, 25), FogEnd = 250 })

	tween(nightDuration, { ClockTime = 24 })
	wait(nightDuration)
	tween(nightDuration, { ClockTime = 6 })
	wait(nightDuration)

	-- Change colors only with 'LightingFix' enabled
	tween(4, { OutdoorAmbient = Color3.fromRGB(140, 140, 140), FogColor = Color3.fromRGB(149, 195, 208), FogEnd = 750 })
end

Module Script:

local globalVariables = {}
--Ready Variables
globalVariables.statsReady = false
globalVariables.gatchimonReady = false
globalVariables.movesReady = false
globalVariables.abilitiesReady = false
globalVariables.itemsReady = false

globalVariables.Ready = false

--Overworld
globalVariables.currentTime = 0
globalVariables.currentWeekDay = 0

--Misc.
globalVariables.progress = nil

--Languages
globalVariables.English = false
globalVariables.LATAMSpanish = false
globalVariables.EUSpanish = false
globalVariables.LATAMPortugese = false
globalVariables.EUPortugese = false
globalVariables.French = false

--Dictionaries
globalVariables.Gatchimon = {
	Stats = { "HP", "STA", "ATK", "DEF", "MA", "SPD" },
}
globalVariables.timesOfDay = { "Dawn", "Morning", "Afternoon", "Evening", "Dusk", "Night" }
globalVariables.weekDays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }

return globalVariables

Hey, could you like provide me all the code files (either .rbxmx or .rbxl files) that do this as I see in ReplicatedStorage you have other things that the script is asking for..?

Also, I don’t know if you realize but you made a duplicate of local WeekDay, you do: game:GetService("ReplicatedStorage") when you already asked for something, this game:GetService() should always be on top.

image

1 Like

nightDuration = 720 seconds and your loop has wait(nightDuration) twice before it can complete, so your loop only runs once every 24 mins… Probably just has not run long enough to see an update? “wait” is deprecated btw.

The issue is that you are tweening the ClockTime after the checks, and the checks don’t run until the cycle resumes from the wait’s. You have to move the ClockTime checks to a separate cycle.

I wasn’t aware wait is deprecated, thanks for pointing that out! What do you recommend I do regarding the loop running once? Would using task.wait() instead be better?

1 Like

Should I use coroutines to create another loop? I remember learning that having multiple loops without coroutines was bad practice, but this could’ve been inaccurate information for all I know

The issue comes from your indexing and how Lua handles arrays. In Lua, arrays start at 1, not 0 — so when your code tries to do:

WeekDay.Value = globalVariables.weekDays[globalVariables.currentWeekDay]

and currentWeekDay is 0, it’s actually returning nil, meaning the value never updates.

To fix, try something like this:

-- Module
globalVariables.currentTime = 1
globalVariables.currentWeekDay = 1

-- Server Script
function changeWeekDay()
	globalVariables.currentWeekDay += 1
	if globalVariables.currentWeekDay > #globalVariables.weekDays then
		globalVariables.currentWeekDay = 1
	end
	WeekDay.Value = globalVariables.weekDays[globalVariables.currentWeekDay]
end

function changeTimeOfDay()
	globalVariables.currentTime += 1
	if globalVariables.currentTime > #globalVariables.timesOfDay then
		globalVariables.currentTime = 1
	end
	TimeOfDay.Value = globalVariables.timesOfDay[globalVariables.currentTime]
end

Both ReplicatedStorage.WeekDay and ReplicatedStorage.TimeOfDay should be StringValues, not NumberValues or IntValues — otherwise, setting .Value = "Morning" or "Sunday" won’t work.

When I set the global variables to 1 instead of 0, I see that the value updates to the second item in the array and not the first (ex: instead of Sunday as the first week day, the value is Monday). By chance, do you know why this is?

that’s because increment is set at 1.

It’s just a matter of when you increase the counter , before or after using it.
Either move the increment to the end or start from 0 again, and your first value will show correctly.