I want to make a sound play when I enter my own server, and learn how to make it.
At the moment, I've gotten this.
[lua]function FirstSpawn ( ply )
if ply:steamid() == "STEAM_0:0:14328487" then
player.GetAll()
ply:ConCommand ("play itisgood.mp3")
end
hook.Add ( "Playerinitialspawn", "playerInitialSpawn", FirstSpawn )[/lua]
Try something more like this, put it in a shared file:
[code]
if (SERVER) then
local function PlaySoundOnJoin(ply)
if (ply:SteamID() == "STEAM_0:0:14328487") then
umsg.Start("PlaySound")
umsg.End()
end
end
hook.Add("PlayerInitialSpawn", "PlaySoundSpawn", PlaySoundOnJoin)
else
usermessage.Hook("PlaySound", function()
surface.PlaySound("itisgood.mp3")
end)
end
[/code]
Okay, so now, I want to make it play 2 sounds at random.
Randomly everytime I join.
So, would I just add math.Random(1,1) if I add surface.playsound("allhail.mp3")
Nope, table random.
[lua]local mySounds = {
"sound0.mp3",
"sound1.mp3"
};
for _,snd in pairs(mySounds) do resource.AddFile("sound/Ops/"..snd); end
function FirstSpawn( ply )
if (ply:SteamID() == "STEAM_0:0:14328487") then
ply:ConCommand("play " .. table.Random(mySounds))
end
hook.Add( "PlayerInitialSpawn", "Velcom", FirstSpawn )[/lua]
Thanks.
Now, about my Resource.addFile problem, check that out, the sounds for this aren't being added.
[QUOTE=Tiagos360;27605739]Nope, table random.
[lua]local mySounds = {
"sound1.mp3",
"sound2.mp3"
};
for _,snd in pairs(mySounds) do resource.AddFile("sound/"..snd); end
function FirstSpawn( ply )
ply:ConCommand("play " .. table.Random(mySounds))
end
hook.Add( "PlayerInitialSpawn", "Velcom", FirstSpawn )[/lua][/QUOTE]
I posted this in your thread, it applies here as well.
[QUOTE=MakeR;27603984]You shouldn't use table.Random on sequentially numerically indexed table (just look at the source to find out why), it is much more efficient to do this:
[lua]function FirstSpawn( ply )
ply:ConCommand("play " .. mySounds[math.random(#mySounds)])
end[/lua][/QUOTE]
But wait, I want to use it for my steam ID.
How do I do that.
[editline]24th January 2011[/editline]
Okay, I'm confused now.
Completely confused.
[QUOTE=MakeR;27605770]Copied from your thread:[/QUOTE]
So ? i didn't say it was mine.And it's still correct
[QUOTE=Tiagos360;27605871]So ? i didn't say it was mine.And it's still correct[/QUOTE]
I'm not saying that you copied that code, I just copied what I said from your thread into this thread because it applies.
I edited my post for clarity.
So, what do I do now?
-automerge-
Basically this sound is meant to play when only the owner (I) join the server.
No one else.
Using Tiagros' method, I'm getting this spammed clientside.
It's either this or my AddFile problem...
[code]Couldn't include file 'shared.lua' (File not found)[/code]
I also changed it abit.
[lua]local mySounds = {
"allhail.mp3",
"itisgood.mp3"
};
for _,snd in pairs(mySounds) do resource.AddFile("sound/"..snd); end
function FirstSpawn( ply )
if ply:steamid() == "STEAM_0:0:14328487" then
ply:ConCommand("play " .. table.Random(mySounds))
end
hook.Add( "PlayerInitialSpawn", "Velcom", FirstSpawn )[/lua]
I'm guessing your wanting other players to hear this sound when you enter the sound. Because this will just play the sound for you.
[LUA]
local mySounds = {
"allhail.mp3",
"itisgood.mp3"
};
for _,snd in pairs(mySounds) do resource.AddFile("sound/"..snd); end
function FirstSpawn( ply )
if ply:steamid() == "STEAM_0:0:14328487" then
for k,v in pairs(player.GetAll())do
v:ConCommand("play " .. mySounds[math.Random(#mySounds)])
end
end
end
hook.Add( "PlayerInitialSpawn", "Velcom", FirstSpawn )
[/LUA]
You mean, when I enter the sound?
No, when I join the game.
[editline]24th January 2011[/editline]
Well what do ya' know.
[code]lua\autorun\allhail.lua:11] 'end' expected (to close to 'function' at line7) near '<eof>'[/code]
Fixed in the edit. No I mean when you enter the game you want everyone to hear this sound so they know you entered.
Yes.
Okay.
[editline]24th January 2011[/editline]
Okay, when I join, I get this... [code]Hook 'Velcom' Failed: [lua\autorun\allhail.lua:8] attempt to call method 'steamid' (a nil value)[/code]
[LUA]
local mySounds = {
"allhail.mp3",
"itisgood.mp3"
};
for _,snd in pairs(mySounds) do resource.AddFile("sound/"..snd); end
function FirstSpawn( ply )
if ply:SteamID() == "STEAM_0:0:14328487" then
for k,v in pairs(player.GetAll())do
v:ConCommand("play " .. mySounds[math.Random(#mySounds)])
end
end
end
hook.Add( "PlayerInitialSpawn", "Velcom", FirstSpawn )
[/LUA]
Now I'm getting this....
[code][lua\autorun\allhail.lua:10] attempt to call field 'Random' (a nil value)[/code]
Change math.Random to math.random
Okay.
Sorry, you need to Log In to post a reply to this thread.