• Garry's Mod Gamemode Music help required!
    7 replies, posted
So I'm trying to get some music to the gamemode that starts playing when a player joins. I have tried searching through the internets but I cant seem to get this to work. in "cl_init.lua" I have written this. [CODE] function GM:PlayerSpawn( ply ) surface.PlaySound( "/sound/propane.mp3" ) end [/CODE] The sound file is located in: SimpleBattles/content/sound SimpleBattles is the Main Folder of my gamemode. Excuse me if I am missing something completly obvious. Im not that experienced in Gmod Gamemode making.
GM:PlayerSpawn is a serverside hook, but you're trying to use it clientside. [URL]http://wiki.garrysmod.com/page/GM/PlayerSpawn[/URL] If you moved the function to a serverside file, you could do something like: [CODE] GM:PlayerSpawn( ply ) ply:SendLua( [[surface.PlaySound( "/sound/propane.mp3" )]] ) end [/CODE] SendLua would make the player run it clientside. There is probably a much better way, but this is just an example.
Oh well that was really dumb of me. What kind of code should I use then to play it on player spawn? Or do I move the code?
I've edited my post to give an example. You can make players run clientside Lua with ply:SendLua. There are other ways to do it, like using a net message sent from the server that makes the player play the sound directly, or using a concommand. But SendLua is probably the easiest way. Also, I just reread your first post. If you want this to only run when the player joins and not every time they spawn, you'll want to change the hook to [B]GM:PlayerInitialSpawn[/B]. It only gets called once the player has loaded in the server. [B]Unfortunately[/B], there is no guarantee that the player has actually fully initialized in the server during PlayerInitialSpawn. They're usually still stuck in the loading menu. As an alternative way, you can keep using [B]clientside [/B]code and do something like this: [CODE] function GM:InitPostEntity() surface.PlaySound( "/sound/propane.mp3" ) end[/CODE] This ensures the player has fully initialized before the sound plays (and not while they're still in the loading menu).
Now It kinda does seem to work but the console tells me "Failed to load sound "content\sound\propane.mp3", file probably missing from disk/repository". Cannot hear any music.
The client doesn't have the file. Therefore you need to send it to them. Or if your just testing, make sure the sound is in the correct file location in your folder.
If im correct, you dont need to have the /sound/ on the beginning because playsound automatically looks in the sound directory. So you would need to change it to surface.PlaySound( "propane.mp3" ) Unless you have a folder at sounds/sounds/propane.mp3 Also dont put a / before sound or at the start of the string.
I went with the new clientside code and now it is still saying that it could not find the file. How do I exactly make sure that i got the right directory? To me it seems alright. Anyways thank you for everyone that has helped me so far! [editline]13th April 2014[/editline] It finally works! Once again a big thanks everyone!:rolleyes:
Sorry, you need to Log In to post a reply to this thread.