• [GMOD HELP] EmitSound keeps playing a bunch of times
    14 replies, posted
I have been having this issue for a while that I just don't understand. I have created working screaming sounds that play when the player is falling. The only issue I am having which I have no clue about is the fact that it plays a bunch of the screams all at once when I fall ONLY is there is more than one player in the server. Here is what I've got: [Lua] [GMOD] GTA 5 Falling Screams Server Anyone have a clue? Thanks in advance.
The problem is that you are repeating it with every player on the server. for k,v in pairs(player.GetAll()) {} do end *If you running it on the server, then you don't have to repeat it again and again, it's enough to play the sound once.
Do not create a new thread about the same code and for such little time if you have another one
So I wouldn't need the 'for loop' to keep checking if the player is falling? Could the hook also cause issues?
The hook "PlayerTick" already looped, so it's already checking when the player falls. Hook should not cause any problems, if we use it correctly. I would not recommend use return in that hook, cause, probably, we will be needed call that hook again. Bellow is how i would redo your code (im dumb af and just learning, so im pretty sure there will be any mistake later) local IsFalling = false  local FallSpeed local MinFallVelocity = 800 sound.Add( { name = "your_added_sound", channel = CHAN_STATIC, volume = 1.0, level = 80, pitch = { 95, 110 }, sound = "ambient/construct_tone.wav" --random one } ) local function FallSounds1(ply) FallSpeed = ply:GetVelocity()[3] if FallSpeed > -MinFallVelocity then IsFalling = false ply:StopSound("your_added_sound") elseif FallSpeed < -MinFallVelocity and IsFalling == false and (ply:Alive()) and not (ply:GetMoveType() == 8) then IsFalling = true ply:EmitSound("your_added_sound") end  end hook.Add( "PlayerTick", "Test", FallSounds1 ) Also, I think the sound of wind should be heard only by the falling player, not by everyone, right?
The wind sounds and screams should be heard by other players as they whoosh by. Awesome, I will have to test this out when my friend gets on.
I think you could just use a bot and enable bot_zombie 1 to disable walking then physgun them off a roof, the rest is self ingenuity
Still doesn't work, I don't know what's up. I've tried getting rid of the 'for loop' and changed the PlayerTick to a Think. Still playing a bunch of times at once.
What K1mer suggested would probably be the best way to do it. Looping through all the players will cause your sound to play multiple times, per player to be precise. The Think function is called every frame which is bad for performance and can overload gmod very quickly. CMoveData/GetVelocity and GM/PlayerTick are the key players here. If it were me I would network it. Have a message sent to the client if IsFalling changes to true, client receives the message and Entity/EmitSound
I tried that out and it didn't seem to work. Same result. SERVER: local function FallSounds1(ply) sound.Add( { name = "fallsound1", channel = CHAN_STATIC, volume = 0.8, level = 64, pitch = 100 * GetConVarNumber("host_timescale"), sound = "michael_falling/fall"..math.random(1,4)..".wav" } ) local MinFallVelocity = 800 FallSpeed = ply:GetVelocity()[3] if FallSpeed > -MinFallVelocity then  IsFalling = false ply:StopSound("fallsound1")  ply:StopSound("fallwind")     elseif FallSpeed < -MinFallVelocity and IsFalling == false and (ply:Alive()) and not (ply:GetMoveType() == 8) then IsFalling = true net.Start("playfall1") net.WriteEntity(ply) net.Broadcast() end end CLIENT: net.Receive("playfall1", function() local ply = net.ReadEntity() ply:EmitSound("fallsound1") ply:EmitSound("fallwind") end)
Remember to precache your sounds, and I just realized you would want to play this serverside from the player themselves if you want everyone to hear it. There are volume options for EmitSound that can make the sound play a very flexible amount of distances: SNDLVL Enumerations So if you want it to work you need to create a global function and call that function when IsFalling = true Players emit sound. (you don't need the think function)
I've tried getting rid of the think hook and having a regular function, but no luck. I tried doing this clientside: if IsFalling == true then LocalPlayer():EmitSound("fallsound1") end but still nothing. It doesn't even play the sound now that I tried that method. I don't even care if it emits the sound from the player to everyone, I just want to get this working at least. I'm just completely baffled on why it won't work right.
Here is a video example of what's exactly happening: https://youtu.be/FQmtNGhjD2k
So the reason you were hearing multiple instances of the sound was due to the hook(s) being called every tick/frame. What you have to do is set a delay to the EmitSound() using CurTime(). What you want to do and the behavior you're expecting has to be called serverside, so forget clientside.
Holy sh*t it works now! Thank you so much! Didn't know it was that simple. Here is my working version: local IsFalling = false FallSpeed = nil local MinFallVelocity = 800 local nextOccurance = 0 local function FallSounds1(ply) sound.Add( { name = "fallsound1", channel = CHAN_STATIC, volume = 0.8, level = 64, pitch = 100 * GetConVarNumber("host_timescale"), sound = "michael_falling/fall"..math.random(1,4)..".wav" } ) local timeLeft = nextOccurance - CurTime() FallSpeed = ply:GetVelocity()[3] if FallSpeed > -MinFallVelocity then  IsFalling = false ply:StopSound("fallsound1") ply:StopSound("fallwind")     elseif FallSpeed < -MinFallVelocity and IsFalling == false and timeLeft < 0 and (ply:Alive()) and not (ply:GetMoveType() == 8) then IsFalling = true ply:EmitSound("fallsound1") ply:EmitSound("fallwind") nextOccurance = CurTime() + 5 end end net.Receive( "ChangedFalling1", hook.Add( "PlayerTick", "vSoundFall1", FallSounds1 ) ) Of course that's not all of my script, this is just one part of many 😉 Once again thank you everyone for your time and feel free to use this part of the script for your own falling sounds.
Sorry, you need to Log In to post a reply to this thread.