Making the function I made global?

Hello,

I’m new to LUA scripting. I’m trying this, but I cant get it to run.

So this is how it is;
This is for a flood server. (in case you dont know the gamemode…) There are 4 phases in the mode, Build(TimerStatus 1), Flood(TimerStatus 2, Fight(TimerStatus 3), RoundRestart(TimerStatus 4).
I want to make it so when a map change occurs, if its on TimerStatus 3, it sets it to 4, then removes all the props, refunds them, then after 15 seconds it changes the map. I think I successfully written the function for it (see below, and please check if its correct).

Flood mod has a customized cl_menu, thus it has its own admin interface including things like kick, ban and slay. I changed this interface to map changer, leaving kick and ban to assmod. I want my admins to change map via this admin interface (not via assmod).

This is the function I’ve written, its on init.lua (I dont even know if its in the right section)

function ChangeIt(maptc)
	RunConsoleCommand("changelevel " .. maptc .. "
")	
end

function MapChg(pl, args)
	if pl:IsAdmin() or pl:IsSuperAdmin() then

		local maptc = args[1]

		if TimerStatus == 3 then
			TimerStatus = 4
			RefundProps()		// a flood mod func that removes and refunds props
			SaveProfile()		// a flood mod func that saves cash
			ReflectTime = RV	// a flood mod var that is needed to switch to timerstatus 4
			timer.Create( "Mapchg", 15, 1, ChangeIt, maptc )
		        for k, v in pairs(player.GetAll()) do  
		            v:PrintMessage(HUD_PRINTTALK, "Map change to " .. maptc .. " in 15 seconds")  
		        end 
		
		else
			RefundProps()		// a flood mod func that removes and refunds props
			SaveProfile()		// a flood mod func that saves cash
			timer.Create( "Mapchg", 15, 1, ChangeIt, maptc )
		        for k, v in pairs(player.GetAll()) do  
		            v:PrintMessage(HUD_PRINTTALK, "Map change to " .. maptc .. " in 15 seconds")  
		        end 
		end
	else
		return false
	end
end
concommand.Add("FLMapChange", MapChg)

With my limited knowledge of lua and bit of a search on google; I’ve set this to a button in admin interface:

FLMapChange(LocalPlayer():GetName(), Value:GetValue())

But it doesnt work, says this in clients console when clicked on the button:


Flood\gamemode\cl_Menu.lua:359: attempt to call global 'FLMapChange' (a nil value)

How can I call this function from init.lua to cl_menu.lua?
Or is there another way to do it?

Thanks in advance,

  • Xev

There are several problems, the first is one of instance. The console command you want to use exists only serverside so you can’t call it directly as a function. The second error is just that, you should not be calling it as a function. You should use RunConsoleCommand() to send trigger it and pass it information.
For instance if you do :
RunConsoleCommand(“FLChangeMap”,mapname)

in the function linked to your console command serverside the arguments will be

  1. the player that sent the command ( no need to specify it)
  2. the command (you don’t really need that)
  3. A table of all arguments.
    If you had called them (ply, cmd, args) to get the map name you would do args[1].

I hope this helps. :smile: See the wiki for more information on these commands.

Hello,

Thanks for the tips. Now the code works but timer does not work. It executes the mentioned function immediately. How can I fix that?

  • Xev

Changed this:

timer.Create( "Mapchg", 15, 1, ChangeIt, maptc ) 

Into this:

timer.Simple( 15, ChangeIt, maptc )  

Both does not wait for 15 seconds but execute the function immediately. What might be the problem?