General > Developers Corner
Admin sounds, tools and other code chat.
spbogie:
Some clerifications of the Lua questions throughout this topic:
--- Quote from: jay209015 on April 07, 2008, 04:40:29 PM ---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.
--- Code: ---function AdminJoin( ply)
if ( !ply:IsAdmin() ) then return end
ply:ConCommand("ulx playsound fivefinger.mp3")
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", AdminJoin );
--- End code ---
--- End quote ---
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:
--- Code: ---function AdminJoin( ply)
if ( !ply:IsAdmin() ) then return end
ply:ConCommand("ulx playsound fivefinger.mp3")
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", AdminJoin );
--- End code ---
Third, your use of
--- Code: ---if ( !ply:IsAdmin() ) then return end
--- End code ---
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.
--- Code: ---function AdminJoin( ply)
if ( ply:IsAdmin() ) then
ply:ConCommand("ulx playsound fivefinger.mp3")
end
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", AdminJoin )
--- End code ---
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.
--- Quote from: jay209015 on April 07, 2008, 04:52:53 PM ---I understand most of your script, but could you explain
--- Code: ---for i, v in ipairs
--- End code ---
plz?
--- End quote ---
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.
--- Code: ---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
]]--
--- End code ---
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.
--- Code: ---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
]]--
--- End code ---
Which you use will depend on the situation.
--- Quote from: Kyzer on April 07, 2008, 05:01:53 PM ---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?
--- End quote ---
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
jay209015:
First of all I would like to thank everyone who replied to my post. Now I am interested in learning lua, but I'm stuck on yet another problem.
--- Code: ---function UseTool( ply, tr, toolmode )
if toolmode == "trails" or toolmode == "ignite" or toolmode == "dynamite" then
if ply:IsAdmin() then
return true
else
return true
Msg( "Player " .. ply:Nick() .. " used " .. toolmode .. "\n" );
end
end
end
hook.Add( "CanTool", "UseTool", UseTool );
--- End code ---
I think by looking at it you can understand my goal here, but it's not doing what I want it to. I want it to say to everyone on the server that some is using Trails, Ignite, or Dynamite tool on the server if they are not admin. I can't seem to get it to display them message. Any hints or suggestions would be great, Thank you in advance.
JamminR:
You're returning before your message. The message line would never get used.
Also, since you're 'returning' within the if is:admin both times, whether they are or not, you don't need both 'returns'.
Just place a single return after the end of the conditional.
This will save a few bytes down the road.
Also, you're using semicolons. Lua allows for those, but they aren't needed for blocks.
They're really only helpful if you place multiple blocks on one line to help tell where one part begins and another ends.
jay209015:
OMG, thank you very much, now I feel retarded :D..
JamminR:
Optimized example (less code, same results)
--- Code: ---function UseTool( ply, tr, toolmode )
if toolmode == "trails" or toolmode == "ignite" or toolmode == "dynamite" then
if not ply:IsAdmin() then
Msg( "Player " .. ply:Nick() .. " used " .. toolmode .. "\n" )
end
return
end
end
hook.Add( "CanTool", "UseTool", UseTool )
--- End code ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version