My module script cannot change the RunSpeed variable

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    My module script does not seem to be able to change the RunSpeed variable for some reason.

  2. What is the issue? Include screenshots / videos if possible!
    This is what i tried to do ↓


    Expected output:
    Time: 12:3:43
    Run speed: 2

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I searched a lot of documents, but I still found nothing.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

this is lua code

local Lighting = game.Lighting

local RunSpeed = 0

local Clock = {
	Hour = 12,
	Minute = 0,
	Second = 0,
}

function ClockMain()
	
	Clock.Second += 1
	
	if Clock.Second == 60 then
		Clock.Second = 0
		Clock.Minute += 1
	end
	
	if Clock.Minute == 60 then
		Clock.Minute = 0
		Clock.Hour += 1
	end
	
	if Clock.Hour == 24 then
		Clock.Hour = 0
	end
end

function WhileLoop()
	coroutine.wrap(function()
		while true do
			wait(RunSpeed + 1)
			
			ClockMain()
			
			print("Time: " .. Clock.Hour .. ":" .. Clock.Minute .. ":" .. Clock.Second)
			print("Run speed: " .. RunSpeed)
			
			Lighting.TimeOfDay = Clock.Hour .. ":" .. Clock.Minute .. ":" .. Clock.Second
		end
	end)()
end

local Main = {
	Main = function()
		WhileLoop()
	end,
	
	SetWorldTime = function(Time)
		
	end,
	
	SetWorldTimeRunningSpeed = function(Speed)
		RunSpeed = Speed <-- This
	end,
}

return Main

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I tried running your script and it worked for me. The issue may be outside of this script?

Now i have no ideas if im correct or not but I still want to share my idea.
I think that the thing might have printed the value before actually changing it.
I tried putting “print(RunSpeed)” right after the value is changed and it printed “2”

The while loop doesn’t have a break. You could break the loop by checking for the max runspeed.

Is this because I put the script in serverscriptservice?

I found that if you use print in SetWorldTimeRunningSpeed, the correct value will be displayed, but it will only output 0 outside of the function. And I have also tried pressing the enter key multiple times

1 Like

I am studying the time system. I use coroutine.wrap to run while, because while will stop the entire code.