# Math.Random help?

I just wanna know a way to make this work. I dont know how unless I replace the whole self:emitsound line and put it in each little choice.

``````

self:EmitSound("vo/" .. math.random(1,7) .. ".wav", 500, 100)
if ( choice == 1 ) then
"heavy_singing01"
if ( choice == 2 ) then
"heavy_singing02"
if ( choice == 3 ) then
"heavy_singing03"
if ( choice == 4 ) then
"heavy_singing04"
if ( choice == 5 ) then
"heavy_singing05"
if ( choice == 1 ) then
"heavy_cheers07"
if ( choice == 1 ) then
"heavy_cheers08"
end )

``````

[lua]
local random = math.random(1,5)
self:EmitSound(“vo/heavy_singing0”…random…".wav",500,100)
[/lua]

[editline]04:45PM[/editline]

Something like that?

But then I can’t use Cheer07/8

[editline]04:33PM[/editline]

``````

function ENT:Initialize()

local selfent = self.Entity

local Timers = { 20, 30, 40, 50, 60, 70, 80 }

local Time = (table.Random(Timers))
local Sound = (table.Random(Sounds))

timer.Create("RandomStuff", Time, 0, function()
random(1,7)

if ( choice == 1 ) then
self:EmitSound("vo/heavy_singing01.wav", 500, 100)
elseif ( choice == 2 ) then
self:EmitSound("vo/heavy_singing02.wav", 500, 100)
elseif ( choice == 3 ) then
self:EmitSound("vo/heavy_singing03.wav", 500, 100)
elseif ( choice == 4 ) then
self:EmitSound("vo/heavy_singing04.wav", 500, 100)
elseif ( choice == 5 ) then
self:EmitSound("vo/heavy_singing05.wav", 500, 100)
elseif ( choice == 1 ) then
self:EmitSound("vo/heavy_cheers07.wav", 500, 100)
elseif ( choice == 1 ) then
self:EmitSound("vo/heavy_cheers08.wav", 500, 100)

end
end )

``````

Its the whole function and it didn’t work. Any ideas?

[lua]local Sounds =
{
Sound( “vo/heavy_singing01.wav” ),
Sound( “vo/heavy_singing02.wav” ),
Sound( “vo/heavy_singing03.wav” ),
Sound( “vo/heavy_singing04.wav” ),
Sound( “vo/heavy_singing05.wav” ),
Sound( “vo/heavy_cheers07.wav” ),
Sound( “vo/heavy_cheers08.wav” )
};

self:EmitSound( table.Random( Sounds ), 500, 100 );[/lua]

Put them in a table.

lua\includes\extensions able.lua:165: bad argument #1 to ‘pairs’ (table expected, got nil)

What?

Post your code if you edited it

You dont really have to use

conserve some memory… anyway. Nevec seems to have done everything right, tell us how you used it, since the problem is coming from you side.

Alright

``````

function ENT:Initialize()

local selfent = self.Entity

local Timers = { 20, 30, 40, 50, 60, 70, 80 }

local Time = (table.Random(Timers))

timer.Create("RandomStuff", Time, 0, function()

self:EmitSound( table.Random( Sounds ), 500, 100 );

local Sounds =
{
Sound( "vo/heavy_singing01.wav" ),
Sound( "vo/heavy_singing02.wav" ),
Sound( "vo/heavy_singing03.wav" ),
Sound( "vo/heavy_singing04.wav" ),
Sound( "vo/heavy_singing05.wav" ),
Sound( "vo/heavy_cheers07.wav" ),
Sound( "vo/heavy_cheers08.wav" )
};

end )

self.Entity:SetModel("models/props_interiors/VendingMachinezombies.mdl")
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )

local phys = self.Entity:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end

end

``````

Shouldn’t that be:
[lua]
local random = math.random(1,5)
self:EmitSound(“vo/heavy_singing0”…tostring(random)…".wav",500,100)
[/lua]

Glua… like any other, reads code from left to right from top to bottom, so it tries to emit the sound but it cant cause local sounds hasint been created yet… put the whole local sounds above emit sound.

Just paste this instead… [LUA]local Sounds = {“vo/heavy_singing01.wav”,“vo/heavy_singing02.wav”,“vo/heavy_singing03.wav”,“vo/heavy_singing04.wav”,“vo/heavy_singing05.wav”,“vo/heavy_singing07.wav”,“vo/heavy_singing08.wav”}

self:EmitSound(table.Random(Sounds), 500, 100)[/LUA]

Your trying to use the Sounds table before you define it, try:

[lua]
function ENT:Initialize()

``````local selfent = self.Entity

local Timers = { 20, 30, 40, 50, 60, 70, 80 }
``````

local Time = (table.Random(Timers))

timer.Create(“RandomStuff”, Time, 0, function()

local Sounds =
{
Sound( “vo/heavy_singing01.wav” ),
Sound( “vo/heavy_singing02.wav” ),
Sound( “vo/heavy_singing03.wav” ),
Sound( “vo/heavy_singing04.wav” ),
Sound( “vo/heavy_singing05.wav” ),
Sound( “vo/heavy_cheers07.wav” ),
Sound( “vo/heavy_cheers08.wav” )
};

self:EmitSound( table.Random( Sounds ), 500, 100 );

end )

``````self.Entity:SetModel("models/props_interiors/VendingMachinezombies.mdl")
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )

local phys = self.Entity:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
``````

end
[/lua]

Wow. That worked fine indeed. Thanks guys!

np

I like to precache all assets before using them, so the game doesn’t randomly freeze. It doesn’t matter if the sound is short, though.

Also, put the Sounds table outside the Initialize function. You’re recreating it every time the timer triggers.

no?

The thing you did, was calling “Sounds” before you defined what it was.

Yeah makes sense now.

``````

choice = math.Random(1,7)
if choice == 1 then
self:EmitSound("vo/heavy_singing01.wav",500,100)
elseif choice == 2 then
self:EmitSound("vo/heavy_singing02.wav",500,100)
elseif choice == 3 then
self:EmitSound("vo/heavy_singing03.wav",500,100)
elseif choice == 4 then
self:EmitSound("vo/heavy_singing04.wav",500,100)
elseif choice == 5 then
self:EmitSound("vo/heavy_singing05.wav",500,100)
elseif choice == 4 then
self:EmitSound("vo/heavy_cheers07.wav",500,100)
elseif choice == 5 then
self:EmitSound("vo/heavy_cheers08.wav",500,100)
end

``````

You could also make a table and then make it randomly select one, like this:

``````
local Sounds = {
"vo/heavy_singing01.wav", -- stuff inside the table
"vo/heavy_singing02.wav",
"vo/heavy_singing03.wav",
"vo/heavy_singing04.wav",
"vo/heavy_singing05.wav",
"vo/heavy_cheers07.wav",
"vo/heavy_cheers08.wav"
}

local Sound = (table.Random(Sounds)) -- Pick a random thing from the "Sounds" table

self:EmitSound(Sound) -- Play the randomly picked thing from "Sounds" table

``````

Or if you want to optimize memory usage :

``````
local Number = math.Random(1,7)
local prefix
if Number > 5 then
prefix = "cheers"
Number = Number + 1
else
prefix = "singing"
end

self:EmitSound("vo/heavy_"..prefix.."0"..Number..".wav") -- Play the randomly composed sound.

``````

I just felt like writting that.