Ulysses
General => Developers Corner => Topic started by: MajorPain931 on September 26, 2014, 03:56:03 AM
-
Hey guys,
I have a little problem, I can't manage to solve. The thing is, I have a server with mapvoting at the end. But when no one's at the server mapvoting results in random maps. Some of them are not that popular (But I want to keep them to vote) which results in less people joining. So I want to make my server automatically change map to a certain rather popular map when no one's ingame and round's over. How can I accomplish this?
I looked around the Hooks, but I didn't find one, which fit my purposes, making it change the map all the time when no one's online would work, but results in high useless cpu usage... I'd like to avoid it. So, a simple script, which checks if players are online (Not the problem, I can do this) but just at the end of the map...
Can some1 help me? I just need a hint, no done script.
Regards MP
-
Use the Player disconnect hook
Check server, no one on? Start a 2(?) minute timer
Playerconnect(or spawn) hook - check for and cancel timer if it exists.
Times up? Check for players again (just to be sure we're not changing and someone is on).
If on, do nothing
If no one is on, change map.
You may need to adjust your timer length if it takes a while for folks to connect between map changes.
Could even do a 10-15 minute just for an 'all clear'
-
Nice, I will give it a try and tell you, if it works (With code, should some1 need same script, too) :)
Thanks
-
Okay, here, what I've written.
I didn't find any command which returns number of players connected (Is there any?), so I had to do it that way.
When map is changed and it's not the defined one, server changes map properly. And when some1 joins timer stops.
But the disconnecting thingy doesn't work... The player disconnects but the function doesn't get called at all? Used "print( "Function got called" )" before the checks to determine it's getting called, but it's not.
But that's not a big deal, since the map just needs about 30 minutes till it's over even if no one is online.
Maybe some1 can refine my code or give me some hints? That's the first real code I wrote in lua and for GMod...
Code:
local changeMap = function()
if (table.Count(player.GetAll()) == 0) then
RunConsoleCommand( "changelevel", "ph_restaurant" )
end
end
local startTimer = function (i)
if (table.Count(player.GetAll()) == i ) then
if(!timer.Exists("mapChanger")) then
if (game.GetMap() != "ph_restaurant") then
timer.Create( "mapChanger", 180, 1, changeMap)
end
end
end
end
local stopTimer = function ()
if (timer.Exists("mapChanger")) then
timer.Remove("mapChanger")
end
end
hook.Add("Initialize", "StartTimerInit", startTimer(0))
hook.Add("PlayerConnect", "StopTimerConnect", stopTimer)
hook.Add("PlayerDisconnected", "StartTimerDC", startTimer(1))
-
table.Count ... player.getall is only way I know to count players.
I found http://wiki.garrysmod.com/page/Player/GetCount (http://wiki.garrysmod.com/page/Player/GetCount) too, but you'd have to specify the "player" type entity name, and I don't know/remember that (or where to find it)
It's also Sandbox and derived only.
Where exactly are you placing the "got called" debug message.
You're passing 1 to the function on disconnect. If that was the last player, (meaning 0(count...getall) != 1(i), it's never going to start that timer.
-
Got it now. Had some strange behaviour with the disconnected hook at first.
The hook got called but getPlayers still returned the disconnecting player in the table. That's why I used "== 1".
But now this doesn't happen anymore, when it gets called, the table is empty. Don't know why it was different at the beginning.
Thanks for helping me, was working fine today and yesterday :)