Vehicle horn keeps going on on press.

Hey there,

So I had a lot of issue’s before about my little horn script.
it works properly right now, but. How to I make the horn only play once while pressing a button?
Current code:


AddCSLuaFile("autorun/dmod_horn.lua")

resource.AddSingleFile("sound/vehicles/car_horn.mp3")
util.PrecacheSound("vehicles/car_horn.mp3")

hook.Add( "Think", "CheckKeyThing", function( ply, key )

	local ply = LocalPlayer()

    if (input.IsKeyDown( KEY_H )) and ply:InVehicle() then
        ply:GetVehicle():EmitSound( "vehicles/car_horn.mp3", 75, 100, 1.0)
    end
end )

Thanks already.

Use

CurTime with some checks and a variable on the player. Example:



local time = CurTime()

if ply.NextHornPress <= time then
	ply.NextHornPress = time + 2 --delay
end


Don’t put this into your code as is. Implement it into the current if statement.

Edit:

From what I can tell, this will only work for whoever is pressing the button. You’re playing a sound clientside. Only the client playing it will hear it. Use my suggestion from your last thread to play it serverside:

That didn’t work at all. How should the code look? And that’s a way of making a delay. I would like to know how to play the sound once at button press.

Which suggestion are you referring to? You should really only be using PlayerButtonDown for this. Also, I’m not going to make your code for you. Show me your code and I’ll tell you how it should look.

Scroll to the top.

Show me your new code where you tried to implement my suggestion.


resource.AddSingleFile("sound/vehicles/car_horn.mp3")
util.PrecacheSound("vehicles/car_horn.mp3")

hook.Add( "Think", "CheckKeyThing", function( ply, key )

	local ply = LocalPlayer()
        local time = CurTime()

    if (input.IsKeyDown( KEY_H )) and ply:InVehicle() and ply.NextHornPress <= time then
	ply.NextHornPress = time + 2 --delay
        ply:GetVehicle():EmitSound( "vehicles/car_horn.mp3", 75, 100, 1.0)
    end
end )

You need to use a default in case ply.NextHornPress is nil.



(ply.NextHornPress or 0)