Author Topic: Message Printing Twice  (Read 390 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 505
  • Karma: 21
    • View Profile
    • iViscosity Gaming
Message Printing Twice
« on: January 03, 2017, 02:11:58 PM »
Code: Lua
  1.  
  2. local white    = Color(    255   ,    255   ,    255   ,    255    )
  3. local red       = Color(    200   ,    25   ,    25   ,    255    )
  4. local green    = Color(    25   ,    200   ,    25   ,    255    )
  5. local blue       = Color(    25   ,    25   ,    200   ,    255    )
  6. local announce   = Color(   0   ,   139   ,   139   ,   255      )
  7. local name_col   = Color(   201   ,   201   ,   0   ,   255      )
  8.    
  9. hook.Add( "PlayerInitialSpawn", "Announce Joins", function( ply )
  10.  
  11.  
  12.    if ply:GetUserGroup() == "superadmin" then
  13.       ULib.tsayColor( _, "", white, "[", announce, "VG Connect", white, "] [OWNER] ", name_col, ply:Nick(), white, " {", red, ply:SteamID(), white, "} has joined the Server." )
  14.    else
  15.       ULib.tsayColor( _, "", white, "[", announce, "VG Connect", white, "] ", name_col, ply:Nick(), white, " {", red, ply:SteamID(), white, "} has joined the Server." )
  16.    end
  17. end )
  18.  
  19.  
  20. gameevent.Listen( 'player_disconnect' )
  21. hook.Add( "player_disconnect", "Announce Leaves", function( data )
  22.    local name = data.name
  23.    local sid = data.networkid
  24.    local bot = data.bot
  25.    local reason = data.reason
  26.  
  27.  
  28.    ULib.tsayColor( _, "", white, "[", announce, "VG Disconnect", white, "] ", name_col, name, white, " {", red, sid, white, "} has left the Server. Reason: " .. reason )
  29. end )

(Ignore the spacing, looks better on my end :P)
This is my entire file, placed in 'addons/VCommands/lua/ulib/modules/vc_connect.lua'.


For whatever reason, when someone leaves the message is printing twice:





Anyone know what's up with this?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline Timmy

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 128
  • Karma: 100
  • Code monkey
    • View Profile
Re: Message Printing Twice
« Reply #1 on: January 03, 2017, 02:58:05 PM »
Your code, seemingly intended for the server-side, is being executed on both the server-side and the client-side.

Scripts inside lua/ulib/modules/ are shared (loaded on both the server-side and the client-side). See what_is_this.txt for more information on where to place your scripts.

As to why it only happens when someone leaves:
PlayerInitialSpawn is a server-side hook and won't get called on the client-side.
player_disconnect is a shared hook and will get called on both sides.
« Last Edit: January 03, 2017, 03:47:10 PM by Timmy »

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 505
  • Karma: 21
    • View Profile
    • iViscosity Gaming
Re: Message Printing Twice
« Reply #2 on: January 03, 2017, 05:17:03 PM »
Ah so I just need to specify one or the other for it to run on?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2445
  • Karma: 375
    • View Profile
    • |G4P| Gman4President
Re: Message Printing Twice
« Reply #3 on: January 03, 2017, 06:57:53 PM »
Yeah, wrap that whole last hook in

Code: [Select]
if SERVER then

code code code

end

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 505
  • Karma: 21
    • View Profile
    • iViscosity Gaming
Re: Message Printing Twice
« Reply #4 on: January 04, 2017, 04:28:52 AM »
Yeah, wrap that whole last hook in

Code: [Select]
if SERVER then

code code code

end

Already did, we're all set. Didn't ever realize shared things were called on both sides, I just thought it meant it could be called on both sides. Oh well, we're all set, thanks.


Edit: Also, is there a way I can access the GetUserGroup() on the disconnect hook? It doesn't use the same format as the InitialPlayerSpawn() so I'm not sure what I need to call it on.
« Last Edit: January 04, 2017, 04:31:26 AM by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline Timmy

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 128
  • Karma: 100
  • Code monkey
    • View Profile
Re: Message Printing Twice
« Reply #5 on: January 04, 2017, 07:24:45 AM »
Edit: Also, is there a way I can access the GetUserGroup() on the disconnect hook? It doesn't use the same format as the InitialPlayerSpawn() so I'm not sure what I need to call it on.

You have to call that GetUserGroup() method on a Player object. The player_disconnect hook does not give you the Player object you need... but it does give you the player’s userid! You can use that id to get the Player object.
Code: Lua
  1. hook.Add( "player_disconnect", "print_group_on_disconnect", function ( data )
  2.   local ply = Player( data.userid )
  3.   print( ply:GetUserGroup() )
  4. end )
« Last Edit: January 04, 2017, 07:45:08 AM by Timmy »

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2445
  • Karma: 375
    • View Profile
    • |G4P| Gman4President
Re: Message Printing Twice
« Reply #6 on: January 04, 2017, 09:34:02 AM »
You could access the ucl table with the id of the disconnecting player and return the group that way. You'd need to print the ucl table to see how it's constructed. I don't remember off the top of my head.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2445
  • Karma: 375
    • View Profile
    • |G4P| Gman4President
Re: Message Printing Twice
« Reply #7 on: January 04, 2017, 09:37:38 AM »
Actually, I just remembered I added something to ULib a while back that would work great for you here..

Code: [Select]
--[[
Function: ucl.getUserInfoFromID

Returns a table containing the name and group of a player in the UCL table of users if they exist.

Parameters:

id - The SteamID, IP, or UniqueID of the user you wish to check.
]]

function ucl.getUserInfoFromID( id )

ULib.checkArg( 1, "ULib.ucl.addUser", "string", id )
id = id:upper() -- In case of steamid, needs to be upper case

if ucl.users[ id ] then
return ucl.users[ id ]
else
return nil
end

end

you could just pass the steamid through this function:

ULib.ucl.getUserInfoFromID( id )

and it'll return a table with the player's name and group.
« Last Edit: January 04, 2017, 11:38:54 AM by MrPresident »

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 505
  • Karma: 21
    • View Profile
    • iViscosity Gaming
Re: Message Printing Twice
« Reply #8 on: January 04, 2017, 10:19:08 AM »
If it returns a table then what do I need to access the group part of it? What's the key?

Sent using Tapatalk. Owner of iViscosity Gaming.

I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2445
  • Karma: 375
    • View Profile
    • |G4P| Gman4President
Re: Message Printing Twice
« Reply #9 on: January 04, 2017, 11:30:05 AM »
print the table to your console and it'll tell you. :)

lua_run PrintTable( ULib.ucl.getUserInfoFromID( "STEAM_0:1:1234567" ) )
« Last Edit: January 04, 2017, 11:35:41 AM by MrPresident »

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2445
  • Karma: 375
    • View Profile
    • |G4P| Gman4President
Re: Message Printing Twice
« Reply #10 on: January 04, 2017, 11:37:51 AM »


The keys are

.name
.group


so to get the group of someone by the steamid, you would simply do:

ULib.ucl.getUserInfoFromID( steamid ).group


Sorry it's so convoluted, it's something we use internally, but it's there if you need it.

It'll also return .allow and .deny which are tables of commands that you have allowed or denied to that player specifically. (not including allows and denys that are in their group)
« Last Edit: January 04, 2017, 11:40:38 AM by MrPresident »

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 505
  • Karma: 21
    • View Profile
    • iViscosity Gaming
Re: Message Printing Twice
« Reply #11 on: January 04, 2017, 11:40:09 AM »
It's fine, I was at school so I couldn't test it

Sent using Tapatalk. Owner of iViscosity Gaming.

I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2445
  • Karma: 375
    • View Profile
    • |G4P| Gman4President
Re: Message Printing Twice
« Reply #12 on: January 04, 2017, 11:41:24 AM »
I figured, since you were using tapatalk. I just want to make sure you are able to get the info you need. :)

Let me know if you need further assistance.

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 505
  • Karma: 21
    • View Profile
    • iViscosity Gaming
Re: Message Printing Twice
« Reply #13 on: January 06, 2017, 09:51:36 PM »
Hm, trying
Code: Lua
  1. if SERVER then
  2.    gameevent.Listen( 'player_disconnect' )
  3.    hook.Add( "player_disconnect", "Announce Leaves", function( data )
  4.       local name = data.name
  5.       local sid = data.networkid
  6.       local bot = data.bot
  7.       local reason = data.reason
  8.  
  9.  
  10.       if ULib.ucl.getUserInfoFromID( sid ).group == "superadmin" then
  11.          ULib.tsayColor( _, "", white, "[", announce, "VG Disconnect", white, "] [OWNER] ", name_col, name, white, " {", red, sid, white, "} has left the Server. Reason: " .. reason )
  12.       else
  13.          ULib.tsayColor( _, "", white, "[", announce, "VG Disconnect", white, "] ", name_col, name, white, " {", red, sid, white, "} has left the Server. Reason: " .. reason )
  14.       end
  15.    end )
  16. end
and Server Console is giving me
Code: [Select]
23:49:35 Lua Error: [ERROR] addons/vcommands/lua/ulib/modules/vc_connect.lua:24: attempt to index a nil value
1. fn - addons/vcommands/lua/ulib/modules/vc_connect.lua:24
  2. unknown - addons/ulib/lua/ulib/shared/hook.lua:109


every time everyone leaves.
« Last Edit: January 06, 2017, 09:53:12 PM by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2445
  • Karma: 375
    • View Profile
    • |G4P| Gman4President
Re: Message Printing Twice
« Reply #14 on: January 07, 2017, 12:15:57 AM »
What is line 24? That error message doesn't help anyone when you only provide a snip of the code.