• Print

Author Topic: Returning Name of a Weapon  (Read 11375 times)

0 Members and 1 Guest are viewing this topic.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Returning Name of a Weapon
« on: October 21, 2014, 04:12:15 pm »
Heyo

I'm working on a script that puts in chat "*Your Name Here*, you are currently holding a *NameOfWeaponYou'reHolding*" as a part of a bigger project
I can't quite figure out how to get the name of the weapon, though

Most recently, I tried:
Code: Lua
  1. if (SERVER) then
  2.         function MyWeaponIsF( ply, text )
  3.                 if ( string.sub(text, 1, 9) == "!myweapon" ) then
  4.                         timer.Simple( 0.1, function() PrintMessage( HUD_PRINTTALK, ply:Name() .. ", you are currently holding a " .. ply:GetActiveWeapon():GetName() ) end )
  5.                 end
  6.                 return false
  7.         end
  8.         hook.Add( "PlayerSay", "MyWeaponIsHook", MyWeaponIsF )
  9. end

All that did was this:


I originally just had ply:GetActiveWeapon() but that didn't work
When I checked the GMod wiki for that, it said it returns an entity, not a string, so I found this Entity.GetName, but that only showed what I showed in the picture above


How would I go about finding  the name of the weapon that a player's holding and have it in string format so I can use it in PrintMessage() ?

Any help is appreciated~

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Returning Name of a Weapon
« Reply #1 on: October 21, 2014, 09:15:30 pm »
weapon:GetPrintName()
Presume that would be used like ply:GetActiveWeapon():GetPrintName()
I'm not sure though.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Returning Name of a Weapon
« Reply #2 on: October 22, 2014, 07:28:05 pm »
Just finished trying that

Unfortunately, it gives the same result as some of my other attempts, which is no result
I type the command and nothing happens

Code: Lua
  1. function MyWeaponIs( ply, text )
  2.         if ( string.sub(text, 1, 9) == "!myweapon" ) then
  3.                 timer.Simple( 0.1, function() PrintMessage( HUD_PRINTTALK, ply:Name() .. ", you are currently holding a " .. ply:GetActiveWeapon():GetPrintName() ) end )
  4.         end
  5. end
  6. hook.Add( "PlayerSay", "MyWeaponIsHook", MyWeaponIs )

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Returning Name of a Weapon
« Reply #3 on: October 22, 2014, 07:43:48 pm »
Tinker.
Try changing order of the command. ply:GetPrintName():GetActiveWeapon()
Try adding additional info (from wiki, I see example of GetActiveWeapon also includes GetClass, which GetPrintName supposedly will convert = ply:GetActiveWeapon():GetClass():GetPrintName()
Try command structure change. ply:GetPrintName( ply:GetActiveWeapon():GetClass() )
Are there server or game console errors?
Is this running on the server or client? (I can't tell from the wiki which it's supposed to be)
Tinker.
Sorry, I don't remember directly, and, tinkering might not only show you how "This" challenge is run, but, teach you something else that might be helpful later.
I know it usually does me when I tinker (Heck, tinkering here taught me there's a GetPrintName command, even if I don't know how to use it, I learned it tinkering)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Returning Name of a Weapon
« Reply #4 on: October 23, 2014, 04:26:11 am »
It's server side Lua, according to the old GMod wiki
I'm running it on a shared file, but it's in the if (SERVER) then statement

I get no errors (client or server) when I run it

I'll try messing w/ it when I get home
« Last Edit: October 23, 2014, 06:19:12 am by Zmaster »

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Returning Name of a Weapon
« Reply #5 on: October 23, 2014, 05:43:40 am »
If the colors on the official GMod wiki are right, then Weapon:GetPrintName() is clientside. (Player:GetActiveWeapon() is shared though).
My GLua is rusty when it comes to serverside and clientside communication, but I assume you could use the Net Library or umsg (user message), since PlayerSay is serverside.
Read MrPresident's reply further down.
« Last Edit: October 23, 2014, 10:14:03 pm by Decicus »
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Returning Name of a Weapon
« Reply #6 on: October 23, 2014, 02:27:12 pm »
Alright

So I found ply:GetWeapons() while looking through DarkRP code, tried to use it, and this is what I ended up with:
Code: Lua
  1. function PrintMessagesForF( ply, text )
  2.         for k, v in pairs( ply:GetWeapons() ) do
  3.                 ply:PrintMessage( HUD_PRINTTALK, v:GetPrintName() )
  4.         end
  5. end
  6.        
  7. function MyWeaponIs( ply, text )
  8.         if ( string.sub(text, 1, 9) == "!myweapon" ) then
  9.                 timer.Simple( 0.1, PrintMessagesForF )
  10.         end
  11. end
  12. hook.Add( "PlayerSay", "MyWeaponIsHook", MyWeaponIs )

When I ran that code, as usual, I typed "!myweapon" and nothing happened
Still no errors
Would that even work? By that, I mean having a hook reference a function which references another function? Not sure if the ply would carry over to the "PrintMessagesForF" function

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Returning Name of a Weapon
« Reply #7 on: October 23, 2014, 04:29:46 pm »
Code: Lua
  1. function MyWeaponIs( ply, text )
  2.         if ( string.sub(text, 1, 9) == "!myweapon" ) then
  3.                 ply:SendLua("LocalPlayer():ChatPrint( LocalPlayer():Nick() .. ', You are currently holding a ' .. LocalPlayer():GetActiveWeapon( ):GetPrintName()) .. '.' ")
  4.         end
  5. end
  6. hook.Add( "PlayerSay", "MyWeaponIsHook", MyWeaponIs )
  7.  

Try that.

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Returning Name of a Weapon
« Reply #8 on: October 23, 2014, 10:14:31 pm »
Code: Lua
  1. function MyWeaponIs( ply, text )
  2.         if ( string.sub(text, 1, 9) == "!myweapon" ) then
  3.                 ply:SendLua("LocalPlayer():ChatPrint( LocalPlayer():Nick() .. ', You are currently holding a ' .. LocalPlayer():GetActiveWeapon( ):GetPrintName()) .. '.' ")
  4.         end
  5. end
  6. hook.Add( "PlayerSay", "MyWeaponIsHook", MyWeaponIs )
  7.  

Try that.

I completely forgot about SendLua. I feel kinda silly now, hah.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Returning Name of a Weapon
« Reply #9 on: October 24, 2014, 04:22:02 am »
Me too hahaha
I'll try that when I get home, thanksies

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Returning Name of a Weapon
« Reply #10 on: October 24, 2014, 03:56:46 pm »
Got a weird error when I ran that code

"[ERROR] LuaCmd:1: unexpected symbol near '..'
  1. unknown - LuaCmd:0"

The file running that code is not named LuaCmd

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Returning Name of a Weapon
« Reply #11 on: October 24, 2014, 09:50:49 pm »
That just means there was an error in the syntax inside the SendLua command.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Returning Name of a Weapon
« Reply #12 on: October 24, 2014, 09:52:33 pm »
Replace the SendLua line with this:
Code: Lua
  1. ply:SendLua("LocalPlayer():ChatPrint( LocalPlayer():Nick() .. ', You are currently holding a ' .. LocalPlayer():GetActiveWeapon( ):GetPrintName() .. '.') ")
  2.  

I messed it up the first time.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Returning Name of a Weapon
« Reply #13 on: October 25, 2014, 08:50:02 pm »
and it works!

Thanks, all of you
You're such a great help

  • Print