Random Events?

How would I get a random event?
Like how can I make something happen at a random time? A set time works but wouldn’t be as good (It’s a last resort) I’m using it for a sent not a gamemode.

Use a timer with a random value:
[lua]
local min = 300 – events are at least 5 minutes apart
local low = 0
local up = 300
local time = math.random(low, up) + min + CurTime() – an event will happen 5-10 minutes from now

timer.Simple(time, SomeFunc)[/lua]

Just to give you an idea.


local min = 10
local low = 0  
local up = 30  
local time = math.random(low, up) + min + CurTime() 
  
timer.Simple(time, Singer)  

function Singer( )

if (self.NextUse or 0) <= CurTime() then  

 local	self:EmitSound("vo/heavy_singing0".. math.random( 1, 5 ) ..".wav", 500, 100)
	
	 self.NextUse = CurTime() + 2 
	 
end 

This won’t work will it?

[editline]07:13PM[/editline]

Gah. I don’t understand need halp. Still learning Lua…

What is it you’re trying to do?

He is trying to make a lua thing that does random things at random times … like the GMT random event system for minigames and things. Mind if i post MY example ?

Yeah, I figured. Yet his code threw me off quite a bit.


local Timers = { 30, 60, 90, 120 }
local Sounds = {"vo/heavy_singing01.wav", "vo/heavy_singing02.wav", "vo/heavy_singing03.wav"}
	
local Time = (table.Random(Timers))
local Sound = (table.Random(Sounds))

timer.Create("RandomStuff", Time, 0, function()
	self:EmitSound(Sound, 500, 100)
end )



Something like that, ye?

http://www.garrysmod.org/img/?t=dll&id=100405

Trying to make this sing :stuck_out_tongue:

[editline]01:45PM[/editline]

I want the entity to emit one of 5 sounds at random intervals between 20-60 seconds.
Monkey’s didn’t work. It had an error emitting the sound. Randomnessness worked fine.

http://img6.imageshack.us/img6/3628/problemnotsolved.jpg

Try


ent:EmitSound(Sound)

instead of


self:EmitSound(Sound)

Did and ent was a nil value I think, I might retry later

try the surface library for sounds

I have other sounds in my sent, like it will make pain sounds when shot. Just a sec I’ll get the error code for both.

init.lua:41: attempt to index global ‘self’ (a nil value)

And

init.lua:41: attempt to index global ‘ent’ (a nil value)

I hate timers they never work for me.
Maybe it thinks self is referring to the timer, but thats just my luanoob idea.

Where have you put the timer?

This is BusyMonkey’s code. I just put in randomly, its not in any functions or anything I know what I’m doing to some degree just not timers.

P.S Love your avatar :smiley:

Put the timer.Simple in ent initialize i suppose, and i agree.

[lua]
timer.Simple(math.random(lowRand,highRand)+min,function(self)

if !ValidEntity(self) then return end
self:EmitSound(whatever,herp,derp)

end,self)
[/lua]

Um
Tried it and it didn’t work (replaced sound with my sound and lowrand and highrand with 20 and 60) I’m pretty sure I did something wrong

[editline]06:56PM[/editline]

DOH DOH DOH DOOOOH DOH DOH DOH DOH DOH COME SING WITH ME, WILLOX!

It worked :smiley: SINGING VENDING MACHINES FOR ALLLLLLL!!!

[editline]07:04PM[/editline]

http://img33.imageshack.us/img33/3497/problemsolvedy.jpg

Your very **un-**optimistic 0.o

Wrong word. Optimistic is being positive about an event that has not yet happened.