• Vehicle horn keeps going on on press.
    7 replies, posted
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: [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 )[/CODE] Thanks already.
Use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/CurTime]CurTime[/url] with some checks and a variable on the player. Example: [code] local time = CurTime() if ply.NextHornPress <= time then ply.NextHornPress = time + 2 --delay end [/code] Don't put this into your code as is. Implement it into the current if statement. [B]Edit:[/B] 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: [QUOTE=AK to Spray;50242959]I can never quite understand why people recommend Think + the input library when you can just use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerButtonDown]GM:PlayerButtonDown[/url] serverside and avoid all of the Lua networking. As a side note, you should probably add a small delay to the horn (if you don't want people to spam it, that is). Example usage of PlayerButtonDown: [code] hook.Add("PlayerButtonDown", "BlaBlaIdentifier", function(pl, btn) if btn == KEY_H then ... end end) [/code][/QUOTE]
[QUOTE=AK to Spray;50262514]Use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/CurTime]CurTime[/url] with some checks and a variable on the player. Example: [code] local time = CurTime() if ply.NextHornPress <= time then ply.NextHornPress = time + 2 --delay end [/code] Don't put this into your code as is. Implement it into the current if statement. [B]Edit:[/B] 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:[/QUOTE] 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.
[QUOTE=DeNaamIsKoen;50262609]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.[/QUOTE] 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.
[QUOTE=AK to Spray;50262653]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.[/QUOTE] Scroll to the top.
[QUOTE=DeNaamIsKoen;50262846]Scroll to the top.[/QUOTE] Show me your new code where you tried to implement my suggestion.
[CODE]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 )[/CODE]
[QUOTE=DeNaamIsKoen;50263060][CODE]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 )[/CODE][/QUOTE] You need to use a default in case ply.NextHornPress is nil. [code] (ply.NextHornPress or 0) [/code]
Sorry, you need to Log In to post a reply to this thread.