ULX

Author Topic: Admin sounds, tools and other code chat.  (Read 51643 times)

0 Members and 1 Guest are viewing this topic.

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Admin sounds, tools and other code chat.
« on: April 07, 2008, 03:42:00 PM »
I was wondering if someone could make a script that would play a sound when an admin joins the server.
I already have to sound I want to use, but I don't have any knowledge of lua. So if anyone could make a
small script it would be greatly appreciated.
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Admin sounds, tools and other code chat.
« Reply #1 on: April 07, 2008, 04:06:18 PM »
Here you go..! =)


Code: [Select]
if SERVER then

function AdminJoins(ply)
if ply:IsAdmin() then
for i, v in ipairs(player.GetAll()) do
v:SendLua("surface.PlaySound(\"npc/turret_floor/active.wav\")")
end
end
end
hook.Add( "PlayerInitialSpawn", "adminjoins", AdminJoins );

end

Simply replace npc/turret_floor/active.wav with whatever the path of your sound file is.
Save it to adminjoin.lua and put it in your lua/autorun folder on your server.
If the sound is custom, make sure you distribute it to your clients, you can do that with ULX in the server.ini file.

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: Admin sounds, tools and other code chat.
« Reply #2 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: [Select]
function AdminJoin( ply)

         if ( !ply:IsAdmin() ) then return end
                             
                             ply:ConCommand("ulx playsound fivefinger.mp3")

         end
 hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", AdminJoin );

**EDIT**
BTW fivefinger.mp3 is a song lol
What does the i, v meain in your scrip?
« Last Edit: April 07, 2008, 04:51:15 PM by jay209015 »
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Admin sounds, tools and other code chat.
« Reply #3 on: April 07, 2008, 04:50:44 PM »
The only thing wrong with it is your tabbing is a bit off.. the end at the bottom should be over to line up with function, not with if. You have your end at the end of the if line already, but it would still run.

Assuming there isn't a problem with having the player run that command, it would work fine.

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: Admin sounds, tools and other code chat.
« Reply #4 on: April 07, 2008, 04:52:53 PM »
I understand most of your script, but could you explain
Code: [Select]
for i, v in ipairs plz?
BTW, I applied for forum membership, and as a member of your community :D
« Last Edit: April 07, 2008, 04:56:20 PM by jay209015 »
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline Chironex

  • Full Member
  • ***
  • Posts: 197
  • Karma: 11
  • Formerly known as Kyzer
Re: Admin sounds, tools and other code chat.
« Reply #5 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?
« Last Edit: April 07, 2008, 05:04:02 PM by Kyzer »

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: Admin sounds, tools and other code chat.
« Reply #6 on: April 07, 2008, 05:03:27 PM »
Thank you for clarifying!
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline PhaxeNor

  • Newbie
  • *
  • Posts: 6
  • Karma: 0
Re: Admin sounds, tools and other code chat.
« Reply #7 on: April 07, 2008, 05:05:18 PM »
Here you go..! =)


Code: [Select]
if SERVER then

function AdminJoins(ply)
if ply:IsAdmin() then
for i, v in ipairs(player.GetAll()) do
v:SendLua("surface.PlaySound(\"npc/turret_floor/active.wav\")")
end
end
end
hook.Add( "PlayerInitialSpawn", "adminjoins", AdminJoins );

end

Simply replace npc/turret_floor/active.wav with whatever the path of your sound file is.
Save it to adminjoin.lua and put it in your lua/autorun folder on your server.
If the sound is custom, make sure you distribute it to your clients, you can do that with ULX in the server.ini file.
If I want this to work with more than just admins, say VIP can I just add ply:IsUserGroup("vip") (from your post) or do I need to modify something first to get the IsUserGroup working?

I want to have it to post a custom Message when a VIP, Admin, SuperAdmin or Server Owner joins

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Admin sounds, tools and other code chat.
« Reply #8 on: April 07, 2008, 05:14:50 PM »
You can add whatever groups you want.. the easiest way for you to do that, would be to do this.

if ply:IsAdmin() or ply:IsUserGroup("groupname") or ... etc

You can add as many groups as you want, just seperate them with 'or'

edit: And Kyzer, I don't believe that storing players.GetAll() in a variable would do anything special.

Offline PhaxeNor

  • Newbie
  • *
  • Posts: 6
  • Karma: 0
Re: Admin sounds, tools and other code chat.
« Reply #9 on: April 07, 2008, 05:16:23 PM »
ok, thank you MrPresident :)

Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
Re: Admin sounds, tools and other code chat.
« Reply #10 on: April 07, 2008, 06:40:45 PM »
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.
Code: [Select]
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:
Code: [Select]
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
Code: [Select]
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.
Code: [Select]
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
Code: [Select]
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.
Code: [Select]
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.
Code: [Select]
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
« Last Edit: April 08, 2008, 03:36:33 PM by spbogie »
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: Admin sounds, tools and other code chat.
« Reply #11 on: April 10, 2008, 06:35:41 PM »
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: [Select]
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 );

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.
« Last Edit: April 10, 2008, 06:38:32 PM by jay209015 »
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Admin sounds, tools and other code chat.
« Reply #12 on: April 10, 2008, 06:48:15 PM »
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.

« Last Edit: April 10, 2008, 06:57:34 PM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: Admin sounds, tools and other code chat.
« Reply #13 on: April 10, 2008, 06:54:21 PM »
OMG, thank you very much, now I feel retarded :D..
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Admin sounds, tools and other code chat.
« Reply #14 on: April 10, 2008, 07:00:47 PM »
Optimized example (less code, same results)
Code: [Select]
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 )
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming