0

I am trying to create an Lua addon for Garry's Mod but I keep coming across an error in my code. This is my code:

function say (Player, text, ent)
    s = "/misc/custom/"..text
    s2 = s..".mp3"
    sound.PlayFile(s2)
end
hook.Add("PlayerSay", "Say", say)

And this is the resulting error.

[saysoundtest25] lua/autorun/chatsounds.lua:4: attempt to call field 'PlayFile' (a nil value)
1. v - lua/autorun/chatsounds.lua:4
2. unknown - lua/includes/modules/hook.lua:84

Any ideas?

Silvio Delgado
  • 6,798
  • 3
  • 18
  • 22
Jake Sharp
  • 11
  • 1
  • 3
  • This means that `sound` exists as an object, but it does not have a member called `PlayFile`. I've never used gmod, so from what you show could it be that some code you run elsewhere has a "sound = something"? Or, less likely (because Lua is not complaining that `sound` doesn't exist, just that it doesn't have `PlayFile` as field), could it be that you need to activate something in the gmod UI or config to make the sound module available? – Oliver Jan 11 '15 at 15:10

2 Answers2

1

User Robotboy655 on Facepunch helped me solve this! The final code:

hook.Add( "PlayerSay", "Say", function( ply, text, team )
BroadcastLua( 'surface.PlaySound("misc/custom/' .. text .. '.mp3")' )
end )

Thanks everyone for the help!

Jake Sharp
  • 11
  • 1
  • 3
0

The /lua/autorun file is serverside and there is a variable sound server-side Lua, however only in client side does sound.PlayFile exist.

if SERVER then
    AddCSLuaFile() -- We're the server running this code! Let's send it to the client
else -- We're a client!
-- Your code here, only ran by the client!
end

See the Garry's Mod wiki for more info and note the orange box on the page which means it's client side.

Please remember to check what the function takes also:

sound.PlayFile( string path, string flags, function callback )

Example (taken from the wiki)

sound.PlayFile( "sound/music/vlvx_song22.mp3", "", function( station )
    if ( IsValid( station ) ) then station:Play() end
end )

Also more easy way of searching the docs:

http://glua.me/

http://glua.me/docs/#?q=sound.PlayFile

How DarkRP handles this:

https://github.com/FPtje/DarkRP/blob/master/gamemode/modules/chatsounds.lua#L275

Codingale
  • 220
  • 6
  • 17
  • @Schollii it's a game, and there is server side only functions and client side functions... in this case `sound.PlayFile` is client-side only and the autorun folder is purely server side. – Codingale Jan 11 '15 at 15:13
  • I've seen a suggestion to use surface.PlaySound. The code: `BroadcastLua('surface.PlaySound(s2)')` almost works, but I end up with an error of `'PlaySound' (string expected, got nil)` If I know the exact file to play, such as "hello.mp3", then the code `BroadcastLua('surface.PlaySound("/misc/custom/hello.mp3")')` will play the sound whenever text is entered in the chat. The tricky part is making it dynamic for different inputs – Jake Sharp Jan 11 '15 at 21:53
  • That means `s2` is not a string, however this should have solved the first issue... this isn't a support form though. It's saying `s2` is `nil` try adding in a few things to debug like print and so on, it should help... – Codingale Jan 12 '15 at 00:11
  • Ah you forgot the path to the file, see how before you had "/misc/custom/"..s2..".mp3"? Yeah sorry just noticed. **Edit** also when I posted that comment your answer came up! Ha, well Robotboy is a dev of gmod so I'd say he could help a lot more than I could, and I suggest using a table or an array for words with a space like "hi there" etc. – Codingale Jan 12 '15 at 00:52