Map sv_gravity changer script

Hi guys, I run a ttt server which by personal choice, I set the sv_gravity to 200 (Players find this fun), but there are the downsides unfortunately, some maps are exploitable and people tend to hide outside these maps. I have had a friend of mine code a little script to assist me with my issue, but his script doesnt seem to work

The code:


local changemaps = {
ttt_mw2_terminal_v1 = true,
ttt_minecraft_b5 = true
}
hook.Add( "Initialize", "ChangeBackMaps", function()
    if changemaps[game.GetMap()] then
        RunConsoleCommand( "sv_gravity", "600" )
    end
end)

I have placed this lua file in: lua/autorun/server/ and it didnt work. I also tried to put it in /lua/autorun/ also that didnt work :frowning: (I saved the file as mapgrav.lua)

Could it be a problem with the code or the placement of the file?

EDIT: To clarify, on those listed maps, I want the gravity to change to 600.



local changemaps = {"ttt_mw2_terminal_v1",
"ttt_minecraft_b5"
}
hook.Add( "Initialize", "ChangeBackMaps", function()
	if (table.HasValue(changemaps, game.GetMap())) then
		RunConsoleCommand( "sv_gravity", "600" )
	end
end)


lua/autorun/server

Set the default gravity to 600 ( Basically don’t set it to 200 ) and use the lua script to change it to 200 if the map is not in the table.

[del]**GM:Initialize **is only called on the server’s initial start up if I remember correctly, meaning the hook gets called for the first map you’re on, but not for every subsequent map change. You’d have to restart the SRCDS process entirely for it to get called again.[/del]

Lolcats’ modification is less optimized (ignoring the use of the Initialize hook problem above) because** table.HasValue **loops through every table element until it finds a match (O(n) time), instead of performing a more efficient hash function to see if the index exists (O(1) time).

GM:Initialize is called on every map start, Lua state is destroyed between changelevels so all hooks are called again if map changes.

My mistake, I thought I had remembered reading otherwise. Thank you for the clarification.

My point still stands in regard to using table.HasValue. Other than being less optimized, it changes nothing in terms of OP’s problem.

Regardless of the fact that it’s being called when it starts, and it’s only being called once, and the difference is literally negligible, The OP didn’t make his problem clear. If he wants those maps to be at 600 gravity, then it’s correct. If he wants those maps to be at 200 gravity, then all he needs to do is change it to 200.

Doesnt work :frowning:

-snip-

?

My exact code:



local changemaps = {"gm_flatgrass",
"ttt_minecraft_b5"
}
hook.Add( "Initialize", "ChangeBackMaps", function()
    if (table.HasValue(changemaps, game.GetMap())) then
        RunConsoleCommand( "sv_gravity", "200" )
        print("Gravity changed to 200!")
    end
end)


Make sure you’re actually saving it as a .lua file too…

table.HasValue is horribly expensive. Just change the maps to be equal to true in the table.

To clarify guys, my default gravity that I have set is 200 in my server.cfg

I want the gravity on the map ttt_minecraft_b5, to be set to 600.

Your original code should work then; does it print that line in console when you change to that map? Otherwise, you could always use Player:SetGravity.

[editline]3rd July 2014[/editline]

The problem might be is that server.cfg is called AFTER GM:Initialize

It doesn’t print :confused:

That makes no sense. Try putting a timer in the initialize hook to make sure the server.cfg is called first.

Pls why timers, I already posted how to solve it :suicide:

He would have to change the hook he’s using then. If the server.cfg is called after, then it would change the value back to the default one no matter what he sets it to in initialize.

If he removes sv_gravity from the config, it will use the default value and it will not override the value set by the script. No need for other hooks, just replace the number and add an ! in the if and you are set.