While loop not running

The loop (attached below) does not seem to run at all. The script has responded to everything outside the loop and below it, but nothing inside it. I’ve come to the conclusion that the loop is not running and hence is the problem. I can’t figure out why.

unknown.idk

P.S. I had it working perfectly fine prior to this. The script is a ModuleScript btw and there is no error I see.
Thanks!

1 Like

You shouldn’t write code other than functions and variables inside a ModuleScript. For actually executing the code, put this into a server script instead. Also, task.spawn exists instead of coroutine.resume coroutine.create.

1 Like

The coroutine isn’t the issue, the loop is. The loop is in a function.

(via return function() --[[script here]] end)

Can you share the code that calls that function?

That’s not the problem, but I’ll comply…

Can you share the whole script and module script with copy and paste? You aren’t being clear enough.

setup/loader

local s, err = pcall(function()
		require(script.Parent.Systems.ServerLocation)()
		require(script.Parent.Systems.ServerUptime)()
	end)
	
	if not s then warn(err) end

serveruptime script

return function()
	print("Name | Loading uptime")
	
	if not workspace:FindFirstChild("name1") then
		workspace:WaitForChild("name")
		if not workspace:FindFirstChild("Name1"):FindFirstChild("Server_Statistics") then
			workspace.name1:WaitForChild("Server_Statistics")
		end
	end
	
	
	local su_folder = Instance.new("Folder", workspace.name1.Server_Statistics)
	su_folder.Name = "Server_Uptime"
	
	local su_value_full = Instance.new("StringValue", su_folder)
	su_value_full.Name = "Full"
	
	local uptime = {
		seconds = 1;
		minutes = 0;
		hours = 0;
	}
	
	print("Name | Loaded uptime (1/3)")
	
	local function update_time()
		local prefixs = {
			seconds = "";
			minutes = "";
			hours = "";
		}
		if string.len(tostring(uptime.seconds)) == 1 then
			prefixs.seconds = "0"
		end
		if string.len(tostring(uptime.minutes)) == 1 then
			prefixs.minutes = "0"
		end
		if string.len(tostring(uptime.hours)) == 1 then
			prefixs.hours = "0"
		end
		
		su_value_full.Value = prefixs.hours.. tostring(uptime.hours).. ":".. prefixs.minutes.. tostring(uptime.minutes).. ":".. prefixs.seconds.. tostring(uptime.seconds)
		print("updated")
	end
	
	print("Name | Loaded uptime (2/3)")
	
	coroutine.resume(coroutine.create(function()
		while wait(1) do
			print(6)
			if uptime.seconds < 59 then
				uptime.seconds += 1
				update_time()
			else
				if uptime.minutes < 59 then
					uptime.seconds = 0
					uptime.minutes += 1
					update_time()
				else
					uptime.seconds = 0
					uptime.minutes = 0
					uptime.hours += 1
				end
			end
		end
	end))
	
	print("Name | Loaded uptime (3/3)")
end

Try calling the functions without pcall and then share the script’s output.


(only related errors)

You must always return a value at the end of a module script. If the pic you posted in the first post is the complete code, you forgot to add a return at the end.

Also, I had a similar problem in the past, where my module scripts would randomly stop working, even if they worked just fine a moment before. I believe it might be a bug on ROBLOX part, and I fixed it by simply copypasting the code onto a brand new module script, while deleting the previous one.

1 Like