• DarkRP: PlaySound on Wanted?
    16 replies, posted
So, basically I'm trying to figure out how to play a short audio clip of some police sirens when a player is wanted. Though I have really no idea how to accomplish that and it's driving me crazy. any Ideas? The code below is what I have so far and it's making me cringe. Please help <3 if (SERVER) then function WANTED(ply) if ply:getDarkRPVar("wanted") then umsg.Start("WOOPWOOP", ply) umsg.End() end end hook.Add( "PlayerWanted", "wanted", WANTED) end if (CLIENT) then usermessage.Hook("WOOPWOOP", function () sound.PlayURL ( "https://www.thesoundarchive.com/montypython/Ni.wav","",function(sound) if IsValid(sound)then sound:Play() end end)  end) end
I think it might be because you're not checking to see if they are wanted you might want to use their hook for player wanted also use https://wiki.garrysmod.com/page/Net_Library_Usage util.AddNetworkString("WOOPWOOP") hook.Add("playerWanted", "playSoundWhenWanted", function(wantedPly, policeOfficer, reason) if wantedPly:GetDarkRPVar("wanted") then net.Start("WOOPWOOP") net.Send(wantedPly) end end) and then all of your client side code I've never played a sound from a URL so I would even for testing purposes try a local HL2 for now to debug. Also sorry if I made any errors not able to test code at the moment.
I got an error & no audio, but atleast it's returning something. :/ LOT: /wanted lot testing [ERROR[ addons/hud/lua/autorun/server/sv_wanted.lua:7: attempted to call method 'GetDarkRPVar' < a nil value > fn - addons/hud/lua/autorun/server.lua:7 Call - addons/ulib/lua/ulib/shared/hook.lua:110 wanted - gamemodes/darkrp/gamemode/modules/police/sv_init.lua:50 callback - gamemodes/darkrp/gamemode/modules/chat/sv_chat.lua:207 callback - gamemodes/darkrp/gamemode/modules/chat/sv_chat.lua:17 RP_PlayerChat - gamemodes/darkrp/gamemode/modules/chat/sv_chat.lua:78 unknown - gamemodes/darkrp/gamemode/modules/chat/sv_chat:143
It's not getting anything for 'GetDarkRPVar' it's returning a nil I've not done a lot with DarkRP modification I just checked the wiki to see if they had a hook Hooks/Server/playerWanted Just double check the format since it's throwing a simple nil error it's most likely a syntax error. And for giggles post all of your code you added into it.
Try getDarkRPVar, I believe the g is lowercase. Im on mobile sorry.
I just checked and it is a lower case.
Well, no errors but also no audio sadly. if SERVER then util.AddNetworkString("WOOPWOOP") hook.Add("playerWanted", "playSoundWhenWanted", function(wantedPly, policeOfficer, reason) if wantedPly:getDarkRPVar("wanted") then net.Start("WOOPWOOP") net.Send(wantedPly) end end) end if (CLIENT) then usermessage.Hook("WOOPWOOP", function () sound.PlayURL ( "http://www.pacdv.com/sounds/voices/yes-2.wav","",function(sound) if IsValid(sound)then sound:Play() end end) end) end
I switched your usermessage to the net library so you need to change the usermessage.Hook to net.Receive so the client would be if (CLIENT) then net.Receive("WOOPWOOP", function(messageLength, pl) if IsValid(pl) and IsPlayer(pl) then // Your sound stuff here end end) end that should resolve it.
Still isn't working, man I have no clue. if SERVER then util.AddNetworkString("WOOPWOOP") hook.Add("playerWanted", "playsound", function(wantedPly, policeOfficer, reason) if wantedPly:getDarkRPVar("wanted") then net.Start("WOOPWOOP") net.Send(wantedPly) end end) end if (CLIENT) then net.Receive("WOOPWOOP", function(messageLength, pl) if IsValid(pl) and IsPlayer(pl) then sound.PlayURL ( "http://www.pacdv.com/sounds/voices/yes-2.wav","",function(sound) if IsValid(sound)then sound:Play() end end)  end end) end
I'll have to test it when I'm home but try and add some print() in to debug see where it's getting caught up.
This seems to work though it constantly keeps overlaping the audio over itself until the game crashes or you're no longer wanted. local function Wanted()   if ply:getDarkRPVar("wanted") then    sound.PlayURL ( "https://www.thesoundarchive.com/montypython/Ni.wav", "", function( station ) if ( IsValid( station ) ) then  station:Play() end   end) end end
when using net.Receive on the client, the only the len arguement is used
--Bump Still having problems with this :/
if SERVER then util.AddNetworkString("WOOPWOOP") hook.Add("playerWanted", "playSoundWhenWanted", function(wantedPly, policeOfficer, reason) net.Start("WOOPWOOP") net.Send(wantedPly) end) else net.Receive("WOOPWOOP", function(_, ply) if IsValid(ply) then sound.PlayURL("http://www.pacdv.com/sounds/voices/yes-2.wav","",function(station) if IsValid(station) then station:Play() end end) end end) end This should work, place it in lua/autorun. If the sound doesn't play then it might be an issue with the sound. Maybe download the sound as MP3 and make the client download it through workshop or something and just use surface.PlaySound instead. Or use this: sound.Add so you can use StopSound when a player is no longer wanted if you plan on playing a looped sound.
Thank you everyone for trying to help. But, sadly it doesn't seem to want to work. I'll try using sound.Add shortly and let everyone know if anything changes. Thanks again, have a good day
I'm autistic, don't worry. Forgot the second argument of net.Receive is nil on the client. if SERVER then util.AddNetworkString("WOOPWOOP") hook.Add("playerWanted", "playSoundWhenWanted", function(wantedPly, policeOfficer, reason) net.Start("WOOPWOOP") net.Send(wantedPly) print("sent", wantedPly) end) else net.Receive("WOOPWOOP", function() sound.PlayURL("http://www.pacdv.com/sounds/voices/yes-2.wav","",function(station) if IsValid(station) then station:Play() end end) end) end This should work. lol.
You're not autistic. It works, thank you <3
Sorry, you need to Log In to post a reply to this thread.