In game clock not working as intended, how to script in Lua?

Hello, I am scripting a clock that appears in the top left corner of the player’s GUI in a text label.

Nothing crazy, just a light practice for coding since I always was bad at it, and I want to learn how to get better by doing it.


image
image

For whatever reason probably syntax, I can’t get the time to print into the text label or into the console output so if anyone knows how to do this I would be greatly appreciative.

6 Likes

In LuaU, there is no need to define types and when defining strings, there is no need to put them in brackets, so I will change this line: local TickTime = "Time in ticks". To my knowledge, there is no function called time, so I will use os.time, so I have changed this line: local TimeNumber = os.time().
There is no need to surround variables in brackets unless you are calling a function, so I have changed this line aswell: Clock.Text = TimeNumber.

The final result is this:

wait(5)
local Clock = game.Players.CrazyGrapefruit32.PlayerGui.ScreenGui.TickDisplay

local TimeNumber = os.time()
local TickTime = "Time in ticks"

while true do
    wait(0.5)
    Clock.Text = TimeNumber
    print(TimeNumber)
    print(TickTime)
end

Please be aware that os.time prints the number of seconds since the Unix Epoch.

6 Likes

here is a quick script i have made that could fix this.

--local script

repeat task.wait() until game.Players.LocalPlayer.PlayerGui:FindFirstChild("TickDisplay", true)

local timenumber = 0
local Clock = game.Players.LocalPlayer.PlayerGui:FindFirstChild("TickDisplay", true)

while task.wait(1) do
       timenumber += 1
       Clock.Text = timenumber
       print( timenumber )
       print( tick() ) --assuming what you meant by "Time in ticks" is this
end
6 Likes

Did you create a variable starting with 0 and used a loop to wait 1 sec and do timenumber++?

3 Likes


also is there a way I can add this little string in the front of the clock?

4 Likes

Yeah, Did you want the timer to start when the server starts or when the player joins?

Because if you want a server clock instead, You can use math.floor(time()).

3 Likes

im not sure yet im just messing around with scripting

for now ill stick with client clock

2 Likes
Clock.Text = "Your time is: "..timenumber
3 Likes

If i did make a server clock would it have to be a regular script in the workspace instead of a local in player scripts?

3 Likes

It doesnt need to be in a server script.

local clocktimer = 0
task.spawn(function()
while task.wait() do
clocktimer = math.floor(time())
--this will return the amount of seconds that has elapsed since the server started in seconds.
end
end) 
5 Likes
Scripting
wait(3)
local player = game:GetService("Players").LocalPlayer
--local Clock = player:WaitForChild("PlayerGui"):WaitForChild("TickDisplay")
--	:WaitForChild("Frame"):WaitForChild("TextLabel")

local rns=game:GetService("RunService")
local Clock=script.Parent -- local clientScript inside the textlable
local startTime = tick()

while true do
	local elapsedTime = tick() - startTime
	local ticks = elapsedTime * 60 -- Assuming 1 tick = 1/60th of a second

	Clock.Text = "Ticks: " .. math.floor(ticks)
	rns.Stepped:Wait() Clock.Text = ""

	print("Elapsed Time:", elapsedTime)
	print("Ticks:", math.floor(ticks))
end
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.