Quick Question

What function do I use to start a timer once the server starts? I think it’s GM:Initialize() but it doesn’t work.

[lua]
function CreateAdvertiser()
adverts = { “BLAH BLAH BLAH”
}

timer.Create( "advertise", 60, 0, function()
	randomnumber = 0
	randomnumber = math.random( 1, table.Count( adverts ) )
	
	advertisement = adverts[randomnumber]

	for k,v in pairs(player.GetAll()) do
		v:PrintMessage( HUD_PRINTTALK, advertisement )
	end
end )

end
[/lua]

P.S. Does it matter where I put this? I put it in lua/autorun/server or w.e

You don’t really need to call this in a function, just put it in a file that would run

Eg, stick it on the end or start of an init.lua, or give it a new file to itself
You could either write it out as a function then call it afterwards, or you could just write it outside of a function, either way should work

Okay, but right now it’s a file itself in lua/autorun/server just like this. How would I make this be called?

I’m pretty sure it should be fine in there, so all you’d need is to change


function CreateAdvertiser()
	<Your code>
end

to


local function CreateAdvertiser()
	<Your code>
end
CreateAdvertiser()

(You don’t need the local, but it’s good practice if you don’t use the function elsewhere)

There are some options. You could create a concommand, maybe hook it to a something. There are probably others that I’m blanking out on now.

Thank you both, I’ll try “my_hat_stinks” way right now.

[editline]19th January 2012[/editline]

Okay so I did it my_hat_stinks way and it didn’t work… Updated code:

[lua]
function local CreateAdvertiser()
adverts = { “BLAH BLAH BLAH”
}

timer.Create( "advertise", 6, 0, function()
	randomnumber = 0
	randomnumber = math.random( 1, table.Count( adverts ) )
	
	advertisement = adverts[randomnumber]

	for k,v in pairs(player.GetAll()) do
		v:PrintMessage( HUD_PRINTTALK, advertisement )
	end
end )

end
CreateAdvertiser()
[/lua]

InitPostEntity is basically Initialize, but it’s called after both the gamemode and the map are loaded.

[lua]
hook.Add(“InitPostEntity”, “Spam Advertisements”, function()
adverts = { “BLAH BLAH BLAH”
}

timer.Create( "advertise", 6, 0, function()
    randomnumber = 0
    randomnumber = math.random( 1, table.Count( adverts ) )
     
    advertisement = adverts[randomnumber]
 
    for k,v in pairs(player.GetAll()) do
        v:PrintMessage( HUD_PRINTTALK, advertisement )
    end
end )

end )
[/lua]

This one works, thanks!

Yeah, when I first started with GMod Lua the first thing I started with was Gamemodes (which some people claim is what you should tackle last, but fuck them…) and I learned that if hooking into Initialize doesn’t work, then hooking into InitPostEntity will probably work. Don’t ask me why; I didn’t make the rules man, I just follow them.

I learned GMod Lua about the same way Fantastic learned how to run Helios One.

There’s your problem, should have been “local function”, as was in my post :stuck_out_tongue:

DERPPPPPPPPPPPPP hahahah