Lua sound coding help

i want to make a server addon that if you type, lets say"omgwtf" it’ll play a sound on the server for everybody to hear is this possible??? if so please help.

[editline]03:10AM[/editline]

plz help

You can not “code” sounds, you need to create them yourself with a microphone and a recording program, (Or get it from somewhere else).


(User was banned for this post ("This isn't helpful" - mahalis))

This is not tested : [lua]function SaySounds( ply, text, toall )
if string.find(text, “omgwtf”) then – If the player said “omgwtf”
ply:EmitSound(Sound(“omgwtf.wav”),500) – Play your sound (The path starts from garrysmod/sounds) at 500% volume
end
end
hook.Add( “PlayerSay”, “SaySounds”, SaySounds )[/lua]

toall < Don’t need

[lua]
function SaySounds( ply, text)
if string.find(text, “omgwtf”) then – If the player said “omgwtf”
for k,v in pairs(player.GetAll()) do – Plays to everyone in server not just to your self
v:EmitSound(“omgwtf.wav”) – Make sure the Sound is in sound/yoursound.mp3 or .wav
end
end
end
hook.Add( “PlayerSay”, “SaySounds”, SaySounds )[/lua]

Omg thank you :smiley: I owe u many thanks lol

[editline]04:15PM[/editline]

Wait where do I put this??

First of all you need a recorded sound (omgwtf.wav) inside your clients’ sound folder. Put that little script in lua/autorun/server

Here’s something a bit more user-friendly:


local chatSounds = {
					{msg = "hi", sound = "hello.wav"},
					{msg = "bye", sound = "goodbye.wav"},
					{msg = "cya", sound = "goodbye.wav"},
}

hook.Add("PlayerSay", "ChatSounds", function(ply, msg)
	for k, v in pairs(chatSounds) do
		if (string.find(msg, v.msg)) then
			ply:EmitSound(v.sound)
		end
	end
end )

Hopefully you should be able to work out how to add more sounds/messages.

(Goes in the same place as infinity posted)

i was using ecabc code and it says on my server
«ŦøĐ» Grimm: omgwtf
Failed to load sound “soundsaysounds\9000.mp3”, file probably missing from disk/repository

That means that the sound file you put there does not exist.

You have to send the file to the clients using resource.AddFile.

where do i put this code


//This requires the "Sourceforts Model Pack"
//Lua File
resource.AddFile("lua/autorun/say_sounds.lua")
//SaySounds Sounds
resource.AddFile("sounds/9000.mp3")

my files is in a addon folder
my file structure is:
addons/say_sounds/info.txt
addons/say_sounds/lua/autorun/say_sounds.lua (does it need to be like addons/say_sounds/lua/autorun/server/say_sounds.lua???)
addons/say_sounds/sound/9000.mp3

how do i make saysounds work on my server?


local chatSounds = {
					{msg = "hi", sound = "hello.wav"},
					{msg = "bye", sound = "goodbye.wav"},
					{msg = "cya", sound = "goodbye.wav"},
}

for k ,v in pairs(chatSounds) do
	resource.AddFile("sound/chatsounds/"..v)
end

hook.Add("PlayerSay", "ChatSounds", function(ply, msg)
	for k, v in pairs(chatSounds) do
		if (string.find(msg, v.msg)) then
			ply:EmitSound(v.sound)
		end
	end
end )

Save that in ‘lua/autorun/server’ somewhere and place sounds in the ‘sound/chatsounds’ folder.

its still not working on my server and im not downloading the files

Are you still trying to do it in addon format?

ya

Try changing:


for k ,v in pairs(chatSounds) do
	resource.AddFile("sound/chatsounds/"..v)
end


to:


for k ,v in pairs(chatSounds) do
	resource.AddFile("addons/addonname/sound/chatsounds/"..v)
end


Personally, for something like this I would just run it as a standalone Lua script in garrysmod/lua/autorun/server.

Ok

[editline]02:57AM[/editline]

can you help me make it work on my server NOT as a addon???

Woobier Change my code to this

This goes in lua/autorun/
[lua]
if (SERVER) then
util.PrecacheSound(“9000.mp3”) – so it downloads the file once
else
function SaySounds( ply, text)
if string.find(text, “omgwtf”) then – If the player said “omgwtf”
for k,v in pairs(player.GetAll()) do – Plays to everyone in server not just to your self
v:EmitSound(“omgwtf.wav”) – Make sure the Sound is in sound/yoursound.mp3 or .wav
end
end
end
hook.Add( “PlayerSay”, “SaySounds”, SaySounds )
end
[/lua]

:wink:

give me an example if i had 2 sounds to emit

Place this code in lua/autorun/server:


local chatSounds = {
					{msg = "hi", sound = "hi.wav"},
					{msg = "bye", sound = "bye.wav"},
					{msg = "cya", sound = "bye.wav"},
}

for k ,v in pairs(file.Find("../sound/chat/*")) do
	resource.AddFile("sound/chat/"..v)
end

hook.Add("PlayerSay", "ChatSounds", function(ply, msg)
	for k, v in pairs(chatSounds) do
		local m_Msg = string.Explode(" ", string.lower(msg))
		
		for x, y in pairs(m_Msg) do
			if (v.msg == y) then
				ply:EmitSound("chat/"..v.sound)
				break
			end
		end
	end
end )

Now any sounds that you use need to go in the ‘sound/chat/’ folder and must follow the name used in the code.