timer.Simple + self.SoundPlay problem.

I’m trying to make it so if the player is on an infected team that it plays a sound, only the player on the infected team himself can hear.
[lua]function Hearsound( ply )
timer.Simple( 15, Hearsound )
if ply:Team() == TEAM_INFECTED then
self.PlaySound( hear_voices[math.random( 1, #hear_voices )] ) --I’ve also tried surface.PlaySound.
else return
end
end[/lua]
Thats the function.

I’ve never messed around with sounds before, but with the hear_voices table I made it should call the sound.

EDIT:
Table just in-case anyone needs it.
[lua]local hear_voices = {
“voices/voice01.mp3”;
“voices/voice02.mp3”;
“voices/voice03.mp3”;
“voices/voice04.mp3”;
“voices/voice05.mp3”;
“voices/voice06.mp3”;
“voices/voice07.mp3”;
“voices/voice08.mp3”;
“voices/voice09.mp3”;
“voices/voice10.mp3”;
“voices/voice11.mp3”;
“voices/voice12.mp3”;
“voices/voice13.mp3”;
“voices/voice14.mp3”;
“voices/voice15.mp3”;
}[/lua]

I defined it at the beginning of the script outside a function (Of course). If I try play voices/voice#.mp3 it works fine, but I’m not so sure as to why the script isn’t playing the sound(s).

Edit:
Just keep messing with things
[lua]function Hearsound( ply )
timer.Simple( 15, Hearsound )
if ply:Team() == TEAM_INFECTED then --Line 29
surface.PlaySound( hear_voices[math.random( 1, #hear_voices )] )
else return
end
end[/lua]
Returns error:


Timer Error: autorun/infected.lua:29: attempt to index local 'ply' (a nil value)

It plays the sound at first, but after that it just returns the timer error.

im tired and about to goto bed but unless im drunk with tiredness this might help. if not just ignore me

ignore the below i know it wont work, at least i think…
[lua]
for k, ply in pairs(player.GetAll()) do
if ply:Team() == “TEAM_INFECTED” then
Hearsound()
else return end
[/lua]

I’ll try it, thanks for helping.

Still returns a timer error:
[lua]function Hearsound( ply )
timer.Simple( 15, Hearsound )
for k, ply in pairs(player.GetAll()) do
if ply:Team() == “TEAM_INFECTED” then
surface.PlaySound( hear_voices[math.random( 1, #hear_voices )] )
else return end
end
end[/lua]


Timer Error: autorun/infected.lua:29: attempt to index local 'ply' (a nil value)

your getting that timer error due to the function trying to get the player when its being called by a timer which isnt a player i believe.

is the script clientside or serverside?

Yeah, I’ve never touched timers before. No idea how to go about it either. Is there any other way I could call that function every 15 seconds?

Script is run serverside.

its all serverside so it should work with ya



for k, ply in pairs(player.GetAll()) do
	if ply:Team() == "TEAM_INFECTED" then
		timer.Create("playzombieshiz", 15, 0, function() //change the 0 to 1 if you want them to hear it only once after 15 seconds
			ply:SendLua("LocalPlayer():EmitSound("..randsound..")")//randsound will be your random sound string thing
			//ply:ConCommand("play \""..randsound.."\"") different way if SendLua goes wrong
		end )
	else return end
end


also since it was serverside script getting the player of console didnt work out to well :stuck_out_tongue: hence the error

Still getting a timer error :frowning:

is the timer error still concerning ply?

whats your line 29 also

maybe if we move the timer.



local randsound = hear_voices[math.random( 1, #hear_voices )]

timer.Create("playzombieshiz", 15, 0, function() 
	for k, ply in pairs(player.GetAll()) do
		if ply:Team() == "TEAM_INFECTED" then
				ply:SendLua("LocalPlayer():EmitSound("..randsound..")")//randsound will be your random sound string thing
				//ply:ConCommand("play \""..randsound.."\"") different way if SendLua goes wrong
		else return end
	end
end )


Fixed it!

Since I kept getting the ply nil value error I just did:
[lua]function Playsound()
surface.PlaySound( hear_voices[math.random( 1, #hear_voices )] )
end
function Hearsound( ply )
if ply:Team() == TEAM_INFECTED then
timer.Create( “playsound”, 5, 0, Playsound )
else return
end
end[/lua]
And it works fine! Just made a seperate function that the timer calls.

well there ya go, just remember that if the server calls the functions the player argument isnt going to work to well.

Hmm.
When I switch teams it continues to play the sounds. How would I make it check?

[lua]
function Playsound()
surface.PlaySound( hear_voices[math.random( 1, #hear_voices )] )
end
function Hearsound( ply )
if ply:Team() == TEAM_INFECTED then
timer.Create( “playsound”, 5, 0, Playsound )
else
timer.Destroy( “playsound” )
end
end
[/lua]

i guess that would work but i think it would make the zombies stop hearing it too…
ahh since its serverside its running as long as somone is infected try

[lua]
timer.Create(“playzombieshiz”, 15, 0, function()
local randsound = table.Random(hear_voices)
for k, ply in pairs(player.GetAll()) do
if ply:Team() == “TEAM_INFECTED” then
ply:SendLua(“LocalPlayer():EmitSound(”…randsound…")")//randsound will be your random sound string thing
//ply:ConCommand(“play “”…randsound…”"") different way if SendLua goes wrong
else return end
end
end )
[/lua]

that lua will check for all the players on infected every 15secs and send them a simple clientside lua if they are infected that will make themselves emit the sound, if you want i can make it where survivors nearby can hear the sounds (i assume are zombie noises) when its played

with my above code you dont need to call it in functions just make it run on startup and itll make the zombies hear the sound clientside

Aha, figured that out. But one problem…
[lua]function Playsound()
surface.PlaySound( hear_voices[math.random( 1, #hear_voices )] )
endfunction Hearsound( ply )
timer.Create( “CheckTeam”, 5, 0, Hearsound )
if ply:Team() == TEAM_INFECTED then
local Infected = 1
else
local Infected = 0
end
if Infected == 1 then
timer.Create( “playsound”, 15, 0, Playsound )
elseif Infected == 0 then
timer.Destroy( “playsound” )
end
end
hook.Add( “PlayerSpawn”, “HearSound”, Hearsound )[/lua]
Edit: Changed it up a bit so theres an Infected variable controlling the timers.

The surface in line 2 of the above is defined as a nil value, so says the timer error. It’s a server/client error though because of the hook. Don’t know how to hook it without causing it to not get shared.

Edit Again:
Damnit! I’m really starting to hate timers and how they don’t like using ply.