Lua script that changes to a preset map for 24 hours every 6 hours.
8 replies, posted
Im looking for a lua script for my server that changes the map every 6 hours, To a map I set. In total I have 4 maps, I want them to rotate every 6 hours in a specific order.
Map_a.bsp
> 6 hours later
Map_b.bsp
> Six hours later
Map_c.bsp
> Six hours later
Map_d.bsp
> 6 hrs later - Resets to Map_a.bsp, Restarting the loop over again.
[lua]-- Our schedule.
local maps = {
["map_a"] = {6, 12},
["map_b"] = {12, 18},
["map_c"] = {18, 24},
["map_d"] = {0, 6}
};
local map = game.GetMap();
timer.Create("Change map according to schedule", 1, 0, function()
local hour = tonumber(os.date("%H"));
for k, v in pairs(maps) do
if(map ~= k and hour >= v[1] and hour < v[2]) then
RunConsoleCommand("changegamemode", k, GAMEMODE.Folder:sub(11));
end
end
end);[/lua]
It works in your server's time so you might want to adjust the schedule if you live in different timezone.
[QUOTE=DarKSunrise;24079230]-- Our schedule.
local maps = {
["map_a"] = {6, 12},
["map_b"] = {12, 18},
["map_c"] = {18, 24},
["map_d"] = {0, 6}
};
local map = game.GetMap();
timer.Create("Change map according to schedule", 1, 0, function()
local hour = tonumber(os.date("%H"));
for k, v in pairs(maps) do
if(map ~= k and hour >= v[1] and hour < v[2]) then
RunConsoleCommand("changegamemode", k, GAMEMODE.Folder:sub(11));
end
end
end);It works in your server's time so you might want to adjust the schedule if you live in different timezone.[/QUOTE]
Thanks for the script. :)
[QUOTE=DarKSunrise;24079230][lua]-- Our schedule.
local maps = {
["map_a"] = {6, 12},
["map_b"] = {12, 18},
["map_c"] = {18, 24},
["map_d"] = {0, 6}
};
local map = game.GetMap();
timer.Create("Change map according to schedule", 1, 0, function()
local hour = tonumber(os.date("%H"));
for k, v in pairs(maps) do
if(map ~= k and hour >= v[1] and hour < v[2]) then
RunConsoleCommand("changegamemode", k, GAMEMODE.Folder:sub(11));
end
end
end);[/lua]
It works in your server's time so you might want to adjust the schedule if you live in different timezone.[/QUOTE]
isnt [url]http://wiki.garrysmod.com/?title=Schedule.Add[/url] a better option?
the schedule library is broken-ish. It doesn't function as it should, at least.
[QUOTE=|FlapJack|;24116980]the schedule library is broken-ish. It doesn't function as it should, at least.[/QUOTE]
There is a addon called Cron(By Lexi) which fixes that doesn't it?
[url]http://www.facepunch.com/showthread.php?t=981910[/url]
[QUOTE=raBBish;23885513][release][b]Cron scheduler[/b]
I suck at making names. Cron is a proper scheduler (unlike the current GMod module) that saves the schedules over sessions.
[url=http://www.garrysmod.org/downloads/?a=view&id=106559][img]http://www.garrysmod.org/img/?t=dll&id=106559[/img][/url]
or if you just want to view it:
[url]http://pastebin.com/xpBBcQMq[/url]
[b]Some examples:[/b]
[lua]cron.RegisterJob( "print", print )
cron.Add( "sched_print", {}, "print", "this runs every minute" )
cron.Add( "sched_print_sun", { Weekday = 1 }, "print", "this runs every sunday" )
-- Removing:
cron.Remove( "sched_print" )[/lua]
[lua]--// Map cleaner
cron.RegisterJob( "cleanup", game.CleanUp )
local conf = {
Minutes = { 0, 30 }
}
cron.Add( "map_cleanup", conf, "cleanup" )
-- or
local conf = {
Minutes = "/30" -- Every 30th minute
}
cron.Add( "map_cleanup", conf, "cleanup" )[/lua]
[lua]--// Example ban system:
cron.RegisterJob( "unban", function( sid )
-- Unban player by SteamID
-- Returning true removes the schedule
return true
end )
function Player:Ban( time )
-- Ban
-- Calculate unban time
local t = os.date( "*t" )
local conf = {
Minutes = (t.min + (time%60))%60,
Hours = (t.hour + math.floor(time/60))%60,
Days = (t.day + math.floor(time/(60*24)))%31,
Months = (t.month + math.floor(time/(60*24*30)))%12
}
-- Create schedule
cron.Add( "unban_" .. self:SteamID(), conf, "unban", self:SteamID() )
end[/lua]
[b]Notes:[/b]
[list]
[*]Jobs must be created every time, as they aren't saved
[*]Schedules are saved, so they only need to be created once
[*]Passed arguments for the job are encoded with glon and saved
[*]Read on Cron syntax [url=http://adminschoice.com/crontab-quick-reference]here[/url]
[list]
[*]Day of week starts from 1 (Sunday)
[*]Range (1-3) isn't implemented
[*]Only numbers, "*" or number with "/" in front ("/30")
[/list]
[*]Leaving a key as nil in config table is the same as it being "*"
[/list]
[b]Installation:[/b]
[list]
[*]Extract to garrysmod/
[/list]
[b]Credits:[/b]
[list]
[*]CapsAdmin for [url=http://www.facepunch.com/showpost.php?p=23880408&postcount=1713]mentioning scheduler[/url] :v:
[/list]
[/release][/QUOTE]
I thought i made that up last night phew haha, was tired at the time.
Well, I made it, not Lexi :3:
[lua]
-- Put this in autorun
require "cron"
local maps = {
"map_a.bsp",
"map_b.bsp"
}
local mapIdx = 1
cron.RegisterJob( "changelevel", function()
game.ConsoleCommand( "changelevel " .. maps[mapIdx] .. "\n" )
mapIdx = ((mapIdx + 1) % #maps) + 1
end )
-- Run this one with lua_run or some command, but only once
require "cron"
local conf = {
Hours = "/6"
}
cron.Add( "MapChange", conf, "changelevel" )[/lua]
Sorry, you need to Log In to post a reply to this thread.