Nested if-then statements

For my SWEP’s secondary fire, I made this:
[lua]function SWEP:SecondaryAttack()
local randnum = math.random(7)

if randnum = 1 then
self:EmitSound( “ohyeah.wav” )
return true
end
elseif randnum = 2 then
self:EmitSound( “HEAVY_killthemall.wav” )
return true
end
elseif randnum = 3 then
self:EmitSound( “SOLDIER_religiousaffilliation.wav” )
return true
end
elseif randnum = 4 then
self:EmitSound( “spy_giveup.wav” )
return true
end
elseif randnum = 5 then
self:EmitSound( “spy_youareall.wav” )
return true
end
elseif randnum = 6 then
self:EmitSound( “SCOUT_regretevrything.wav” )
return true
end
elseif randnum = 7 then
self:EmitSound( “SOLDIER_buttescortedfrombank.wav” )
return true
end
end[/lua]

However, when I opened GMod, it told me that I needed a “then” near an “=”. What did I do wrong?

You need to use == to check for equality, the = operator is for setting variables to values.

[editline]09:53PM[/editline]

[lua]if randnum == 1 then[/lua]

Oh, thank you. /thread

Also you have an end too many for every elseif.

[editline]09:59PM[/editline]

You need to remove the ends after each return true

Yeah, you only put the end at the very end of your if thing.

if … elseif … elseif … elseif … else … end

thanks!

And something that’ll save you a lot of time: use tables. Please.

[lua]
local snds = {
“ohyeah.wav”,
“HEAVY_killthemall.wav”,
“SOLDIER_religiousaffiliation.wav”,
“spy_giveup.wav”,
“spy_youareall.wav”,
“SCOUT_regretevrything.wav”,
“SOLDIER_buttescortedfrombank.wav”}

function SWEP:SecondaryAttack()

self:EmitSound( table.Random(snds) )
return true

end
[/lua]