Game Time Script

a script that does not show the normal time but the game time
Hours are minutes and seconds are minutes

Thanks

What do you exactly mean by “game time” and “normal time”?

I think he’s saying instead of 1 hour being an actual hour that 1 hour is actually 1 minute

Yes

[lua]
–“game time” clock
local thetime = RealTime()
local seconds = math.floor((thetime * 60) % 60) --seconds are “fake”
local minutes = math.floor(thetime % 60) --minutes are the new seconds
local hours = math.floor((thetime / 60) % 60) --hours are the new minutes
local days = math.floor(thetime / 3600) --days are the new hours
[/lua]

Should give you what you’re looking for.

Thanks is this the same of all the players? when i create a hud?

For a consistent time across all players, you should declare those variables on the server and then either network them or send them over to the player every tick or so to update the clocks on their HUDs.

Mentioning that, please don’t forget that you need to re-set these variables every tick or so, or they won’t update.

can you give me a example

<snip>
Terrible example. Reposted below.

Why not use a Think hook, then use a variable set to CurTime()
[lua]
local tick = CurTime()
hook.Add(“Think”,“Custom.UpdateTime”,function()
if tick + 1 < CurTime() then
for k,v in pairs(player.GetAll()) do
v:SendLua("")
end
end
end)
[/lua]

Oh yeah, I forgot that I could loop through players like that. Oh haha, that was just stupid of me.
I used Tick because it’s uniform to client and server and because it’s only called when there are users connected. There’s no point in running the function if there’s no one to benefit from it.

I used RealTime because it’s a more concrete time. When used on the server it is the time since it has been turned on. CurTime can’t be used for a clock because it only runs when players are present, and that brings us to the “if a tree falls in a forest and no one is around to hear it, does it make a sound” issue. Time is forever. I could have probably used SysTime as a substitute. Less large a number (especially on servers that basically never shut down).

Thanks guys but how can i buld the hud

If you want someone to build you a HUD with the game-time built into it, we’ll need more detail than that. If you honestly don’t know how to make a HUD and just want to know that, here are some links:

HUD Tutorials:

http://wiki.garrysmod.com/?title=How_to:_HUD (Useful mostly for its HUD element-hiding setup, very clean)

Useful Libraries:

[editline]19th January 2012[/editline]

Also, redid the example with usermessages and much cleaner code.
To use, serverside in lua/autorun/server/, the other in lua/autorun/client/.

sv_gameclock.lua
[lua]
local thetime = {}
thetime.base = 0
thetime.seconds = 0
thetime.minutes = 0
thetime.hours = 0
thetime.days = 0

local function update_time()
thetime.base = SysTime()
thetime.seconds = math.floor((thetime.base * 60) % 60)
thetime.minutes = math.floor(thetime.base % 60)
thetime.hours = math.floor((thetime.base / 60) % 60)
thetime.days = math.floor(thetime.base / 3600)

umsg.Start("gameclock")
	umsg.Short(thetime.seconds)
	umsg.Short(thetime.minutes)
	umsg.Short(thetime.hours)
	umsg.Short(thetime.days)
umsg.End()

end

hook.Add(“Tick”, “gameclocktick”, update_time)
[/lua]

cl_gameclock.lua
[lua]
local cltime = {}
cltime.seconds = 0
cltime.minutes = 0
cltime.hours = 0
cltime.days = 0

usermessage.Hook(“gameclock”, function(msgdata)
cltime.seconds = msgdata:ReadShort()
cltime.minutes = msgdata:ReadShort()
cltime.hours = msgdata:ReadShort()
cltime.days = msgdata:ReadShort()
end)

local function draw_HUD()
draw.RoundedBox(1, ScrW() - 210, 10, 200, 25, Color(0, 0, 0, 150))
draw.DrawText(“Day: “…cltime.days…” - “…“Time: “…cltime.hours…”:”…cltime.minutes…”:”…cltime.seconds, “ChatFont”, ScrW() - 195, 15, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT)
end

hook.Add(“HUDPaint”, “gameclockHUD”, draw_HUD)
[/lua]

Error:
Warning: Unhandled usermessage ‘gameclock’

Works fine when I use it. Make sure you’re running it in Multiplayer.

You never AddCSLuaFile’d cl_gameclock

You shouldn’t need to when running it locally (Start New Game -> Multiplayer). That was just an example, not something to use on an actual server D:

I uploadt on my server

Can you mabye helpe me to create it for a server?

See, that’s it. If you use that on a server, add this to the top of the serverside file:
[lua]AddCSLuaFile(“autorun/client/cl_gameclock.lua”)[/lua]

Is that all you wanted? Or did you have something bigger in mind?

[lua]
AddCSLuaFile(“autorun/client/cl_gameclock.lua”)
local thetime = {}
thetime.base = 0
thetime.seconds = 0
thetime.minutes = 0
thetime.hours = 0
thetime.days = 0

local function update_time()
thetime.base = SysTime()
thetime.seconds = math.floor((thetime.base * 60) % 60)
thetime.minutes = math.floor(thetime.base % 60)
thetime.hours = math.floor((thetime.base / 60) % 60)
thetime.days = math.floor(thetime.base / 3600)

umsg.Start("gameclock")
    umsg.Short(thetime.seconds)
    umsg.Short(thetime.minutes)
    umsg.Short(thetime.hours)
    umsg.Short(thetime.days)
umsg.End()

end

hook.Add(“Tick”, “gameclocktick”, update_time)
[/lua]

Now it wil works i wil test it and Wizardsbane can i mabey add you on steam?