[Solved] How to only use a certain part of an already set value

I am currently trying to make an analog clock that reflects EST time. I’m okay with lighting showing 24-hour time, but I am not okay with the analog clock showing it. I am working on a system for coordinating that, but the problem is, I don’t know how to “chop off” the seconds part of the lighting.TimeOfDay value.

image

Script:

local textLabel = script.Parent

local serverScriptService = game:GetService("ServerScriptService")
local values = serverScriptService:FindFirstChild("Values")

local TimeIsAM = values.TimeIsAM
local GameTime = values.GameTime
local GameClockTime = values.GameClockTime

while true do
	wait()
	if TimeIsAM then
		textLabel.Text = GameTime.Value .. " AM"
	else
		textLabel.Text = GameTime.Value .. " PM"
	end
end

I want to make the textLabel.Text value the hours and minutes part of the lighting.TimeOfDay value. I have tried searching it up and using other models, but have found no luck.

Example:

textLabel shows: 12:35
lighting.TimeOfDay shows: 12:35:24

(GameTime.value is set to lighting.TimeOfDay)

I think that string.gsub might solve your problem.

You could use string.split

local textLabel = script.Parent

local serverScriptService = game:GetService("ServerScriptService")
local values = serverScriptService:FindFirstChild("Values")

local TimeIsAM = values.TimeIsAM
local GameTime = values.GameTime
local GameClockTime = values.GameClockTime

while true do
	wait()
	local Time = string.split(GameTime.Value, ":")
	if TimeIsAM then
		textLabel.Text = string.format("%u:%u AM", Time[1], Time[2])
	else
		textLabel.Text = string.format("%u:%u PM", Time[1], Time[2])
	end
end

I didn’t even know that string.split was a thing! this is a much better way at completing this task.

I got this error when I tried to use your solution:

Workspace.LargeDigitalClock.TimePart.SurfaceGui.TextLabel.ShowRealTime:16: invalid argument #2 to 'format' (number expected, got string)

I have fixed the code from my answer.