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

Current time module gui maker (NOT WORKING?)

i am made this code that would make instant time guis but every single time this happens *I get no errors *JUST NOT WORKING??

function module.create_time_gui(env)
	local main = Instance.new('TextLabel',env)
	
	--Layout code
	main.Size = UDim2.new(0, 115,0, 41)
	main.Position = UDim2.new(1, -160,1, -44)
	main.TextColor3 = Color3.new(1, 1, 1)
	main.BackgroundTransparency = 1
	main.TextScaled = true
	main.Font = Enum.Font.Ubuntu
	
	--Time code
	while wait() do
		local main_data = os.date("*t")
		main.Text = (main_data.hour..':'..main_data.min)		
	end
	
	return(main)
end

what did i do wrong with my code?

1 answer

Use a thread, while wait() do is a loop that yields. Thus, it will not run the code you input after the loop. Coroutines help with this problem.

Edit: Modified!

function module.create_time_gui(env)
	local main = Instance.new("TextLabel") --Parent argument is not recommended, as it already creates the Instance before setting the property

	--Layout code
	main.Size = UDim2.new(0, 115,0, 41)
	main.Position = UDim2.new(1, -160,1, -44)
	main.TextColor3 = Color3.new(1, 1, 1)
	main.BackgroundTransparency = 1
	main.TextScaled = true
	main.Font = Enum.Font.Ubuntu

	--Time code
	coroutine.resume(coroutine.create(function() --We create an anonymous thread and resume will already run the thread
		while wait() do
			local toformat = "%I:%M" --A string to format. %I is 12-hour clock and %M is the minutes.
			local main_data = os.date(toformat, os.time()) --We will format everything in place!
			main.Text = main_data
		end
	end))

	main.Parent = env --Let's set it last!

	return main
end

3 comments

0

Seems to not work

0

It is stuck at 1 time and only one time

0

I'll try fixing it!

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