Some clerifications of the Lua questions throughout this topic:
Wow, thx for the quick response. I made my own script, can you tell me if it's ok. If not could you tell me what's wrong with it and why, so I can learn lua.
function AdminJoin( ply)
if ( !ply:IsAdmin() ) then return end
ply:ConCommand("ulx playsound fivefinger.mp3")
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", AdminJoin );
Jay, your code should function just fine, however there are a few things I would like to point out.
First, the semicolon ( ; ) after hook.Add(...) is not needed. Doesn't hurt anything, just not needed.
Second, is some standard indentation guidelines. You want to indent when you are stepping into a new block (ie. after function blah (), then, do, etc...) and then step the indentation back out after you close that block (end).
Here is your code with standard indentation:
function AdminJoin( ply)
if ( !ply:IsAdmin() ) then return end
ply:ConCommand("ulx playsound fivefinger.mp3")
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", AdminJoin );
Third, your use of
if ( !ply:IsAdmin() ) then return end
is perfectly valid, and will give you the intended result (though personal preferences is to use the Lua standard "not" keyword over rather than the ! added by garry), I just want to show you another way it could have been written (you may know this already) that will eliminate the not operation.
function AdminJoin( ply)
if ( ply:IsAdmin() ) then
ply:ConCommand("ulx playsound fivefinger.mp3")
end
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", AdminJoin )
Of course, that entire if statement could also be condensed onto one line if desired, but spliting it up will tend to make it easier to read. Be careful of putting in too much whitespace as well though.
I understand most of your script, but could you explain for i, v in ipairs
plz?
That for loop syntax is used to loop through a table. i and v are variables that will store the data from the table for each execution of the loop. i is the index/key, v is the value. ipairs creates an iterator form the given table.
There are 2 functions for creating such iterators. pairs and ipairs. ipairs is used to loop through a sequential numeric table where i will be the index in the table, and v the value stored at that index.
t={"a","b","c","d"}
for i, v in ipairs( t ) do
print( i, v )
end
--[[ Output will look something like this:
1 a
2 b
3 c
4 d
]]--
pairs is used to loop through a table with non-sequential keys (a key can be almost anything). When using pairs there is no guarentee as to the order that the data will come out.
t = {}
t[3]="a"
t["b"]="c"
t[-50]="d"
for k, v in pairs( t ) do -- variables can be called anything. I tend to use k for key and v for value.
print( k, v )
end
--[[ Sample Output:
-50 d
3 a
b c
]]--
Which you use will depend on the situation.
His code play the sound for everyone, while yours just play it for yourself (the admins)
As he don't use 'i', don't care about it, and 'v' will be each player, as players.GetAll() return a table of connected players.
Out of subject: I'm not Lua expert, but isn't it better to store players.GetAll() in a new variable, instead of calling the function at each iteration of the loop? Or it's already stored at first iteration with Lua?
His code will play the sound for all users aswell. It just does so by running the "ulx playsound" command from the admin's console instead.
As for players.GetAll(), it is only called once during the execution of the code. On that first call it will return a table of all players which is passed to ipairs to create an iterator for the for loop.
For more general Lua information, please check out the tutorials on the Lua-users wiki at
http://lua-users.org/wiki/TutorialDirectory