• Print

Author Topic: Lots of Questions  (Read 4563 times)

0 Members and 1 Guest are viewing this topic.

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Lots of Questions
« on: February 05, 2014, 09:18:30 pm »
I tried to use LocalPlayer(), but I don't seem to be using it correctly.

How does LocalPlayer() work?
And how does the EmitSound here differ from the regular EmitSound?

Please don't link me to this.

Code: Lua
  1. -- This will play sounds client side
  2. local function rcvSound( um )
  3.         local str = um:ReadString()
  4.         if not ULib.fileExists( "sound/" .. str ) then
  5.                 Msg( "[LC ULib ERROR] Received invalid sound\n" )
  6.                 return
  7.         end
  8.  
  9.         if LocalPlayer():IsValid() then
  10.                 LocalPlayer():EmitSound( Sound( str ) )
  11.         end
  12. end
  13. usermessage.Hook( "ulib_sound", rcvSound )
« Last Edit: February 05, 2014, 09:25:33 pm by Neku »
Out of the Garry's Mod business.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Lots of Questions
« Reply #1 on: February 05, 2014, 10:24:06 pm »
LocalPlayer() always returns the player's entity clientside. You can see other players' entities clientside too, but LocalPlayer() will always be you. How did you try to use it before?

EmitSound is shared. It has the same function on server and client, but if you call it clientside, only the client it is called on will hear it. If you omit the second 2 parameters (volume and pitch) they just default to normal.

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Lots of Questions
« Reply #2 on: February 05, 2014, 10:55:17 pm »
Code: Lua
  1. local function AddToScript( str ) -- This adds whatever sound is to be played to a script.
  2.         if not str then
  3.                 return
  4.         end
  5.         sound.Add( {
  6.         name = "round_sound",
  7.         channel = CHAN_STATIC,
  8.         volume = 1.0,
  9.         level = 0,
  10.         pitchstart = 100,
  11.         pitchend = 100,
  12.         sound = str
  13.         } )
  14. end
  15.  
  16. local function RoundSound( str ) -- Plays the round sound.
  17.         AddToScript( str )
  18.         currentlyplayingsound = true
  19.                 if LocalPlayer():IsValid() then
  20.                         LocalPlayer():EmitSound( Sound( "round_sound" ) )
  21.                 end
  22.         end
  23. end
  24.  
  25. local function StopRoundSound() -- Stops JUST the round sound.
  26.         if not currentlyplayingsound then
  27.                 return
  28.         else
  29.                 currentlyplayingsound = false
  30.         end
  31.                 if LocalPlayer():IsValid() then
  32.                         LocalPlayer():StopSound( "round_sound" )
  33.                 end
  34.         end
  35. end

I was doing this. It's definitely not giving the expected result.
Out of the Garry's Mod business.

  • Print