Changing gamemode based on the next map in rotation

Hey guys. I’ve been playing Garry’s Mod for a while and have recently started my own server. I have a little bit of lua experience and know enough to keep the server running well. There is just one thing I cannot seem to figure out and that’s how to change the gamemode from one mode to another based on what map is coming up next.
I’ve tried using if/else statements to read the map that’s loaded and alter the gamemode based on that, but I don’t know if it’s just not working or if it’s not executing correctly. I have it saved in garrysmod/lua/autorun/server and this is what I have in it.


function gamemodes()
    if ( string.find( string.lower( game.GetMap() ), "bhop_" ) ) then RunConsoleCommand( "gamemode bhop" )
	else ( RunConsoleCommand( "gamemode deathrun") end
end)

I’m attempting to get the mapname and load the gamemode based on that, but I feel like this will execute after the map loads anyway so it won’t have any effect on the server until another map change. Can anyone help me out with this? I don’t want you to do it for me, I want you to help me.

Why don’t you order it like the old fretta vote. Pick gamemode ===> Pick map of that gamemode

But that’s not what I’m trying to do. In my mapvote, I want it to be the normal 5 maps but both DR and BHop maps. Then I want the server to check the map coming next and if the first part of the name “dr_/bhop_” and start the gamemode according to what it reads. Fretta voting would work similarly, but I feel like it would accomplish less than doing it like this.

why not set the gamemode when the map is chosen?
Simply ID what gamemode is used by what maps and if that map is chosen then change the gamemode to that before the map changes.

That’s basically what I’m trying to do here. I just don’t know how to execute it before the mapchange occurs. Would the code I have there work (with a few alterations, primarily the game.GetNextMap instead of GetMap)?

changelevel <mapname> <gamemodename>

But I’m wanting a way for the server to automatically recognize the map prefix and set the gamemode based on that. I’m trying to figure out where I should place the lua code to do this. Could you possibly point me a bit farther?

I did something similar to this a while ago, I’ll see if I can find it. It may need a little editing, though because of updates and stuff.

It would be much appreciated, dude. Even if I need to update it and fix some stuff, it would be a good help.



function gamemodes()
    if ( string.find( string.lower( game.GetMap() ), "bhop_" ) ) and not gamemode.Get("bunnyhop") then 
		RunConsoleCommand( "gamemode", "bhop" ); 
		RunConsoleCommand( "changelevel", game.GetMap()) 
	elseif gamemode.Get("deathrun") then
		RunConsoleCommand( "gamemode", "deathrun"); 
		RunConsoleCommand( "changelevel", game.GetMap())  
	end
end

hook.Add("Initialize", "LoadMyScript", function()
	gamemodes()
end)


Untested, should work, bad method, I recommend that you actually do a check before the map changes, this is a horrible method just letting you know, the reason I did this method for you, is because I don’t know how your handling end game stuff… This should work but it isn’t recommended by me.

If you run the command before the changelevel; you may notice it try to load the game-mode and cause the game to freeze.

The principal is right, but with the concommand just add the game-mode as an additional command based on the map. If you have a map-vote script or map cycle system then just edit the code to see if it starts with x, y or z…

I changed yours a bit:
[lua]function ChangeMap(map)
if ( string.find( string.lower( map ), “bhop_” ) ) and gamemode.Get(“bunnyhop”) then – This checks if its a bunnyhop map and if we have the bunnyhop gamemode
RunConsoleCommand( “changelevel”, map, “bhop”)
elseif gamemode.Get(“deathrun”) then
RunConsoleCommand( “changelevel”, map, “deathrun”)
end
end[lua]
Untested but by going off of Acecool’s suggestion it should work.

I did read your suggestion, and I tried it.

This is why I did it that way.

run “gamemode darkrp” before doing changelevel.

I feel like a giant noob, but I can’t seem to figure out where to put this. No matter where it goes it doesn’t seem to work. Even while using the SimpleMapVoting addon and altering the way it handles changing maps doesn’t do anything.
Should I be modifying the addon or should I create a separate lua file for this?

go to lua/autorun/server
make a file called lalalalala.lua
then paste it in, save it.

[lua]local GamemodeList = engine.GetGamemodes()local MapPatterns = {}

for k, gm in pairs( GamemodeList ) do
local Name = gm.name or “sandbox”
local Maps = string.Split( gm.maps, “|” )

if ( Maps && gm.maps != "" ) then
	for k, pattern in pairs( Maps ) do
		MapPatterns[ pattern ] = Name
	end
end

end

function ChangeMap(map)
local Gamemode = “sandbox”
local name = string.gsub( map, “.bsp”, “” )
local lowername = string.lower( map )

for pattern, Game in pairs( MapPatterns ) do
	if ( ( string.StartWith( pattern, "^" ) || string.EndsWith( pattern, "_" ) || string.EndsWith( pattern, "-" ) ) && string.find( lowername, pattern ) ) then
		Gamemode = Game
	end
end
RunConsoleCommand( "gamemode", Gamemode ); 
RunConsoleCommand( "changelevel", map) 

end[/lua]
That code should automatically set the gamemode and change the map for you when you call ChangeMap(nameofmap)
It works just like how the map menu sorts maps