I've spent hours trying to get this function to work right... it's in the cl_init as it's part of the hud...
This is for a low health alert thing.. for a gamemode I'm trying to make.
[code]
function alert()
local client = LocalPlayer();
local hpm = 100;
NextTime = 0
if (CurTime() > NextTime) then
if (client:Health() <= hpm*(1/4))
then
RunConsoleCommand("play", "lhalert")
end
NextTime = CurTime() + 1
end
end
hook.Add("Tick", "alert", alert);
[/code]
I'm trying to make it play a sound only the localplayer can hear... and since I'm partially new to Lua (I used a bit of another hud to get this working) I tried using the console command Play "sound"..
How ever when I test the code it plays the sound for a millisecond before playing it again and it keeps going till I heal or die..
What is it that I'm doing wrong?
You reset NextTime to 0 every tick.
[QUOTE=Overv;17119657]You reset NextTime to 0 every tick.[/QUOTE]
It does.
[QUOTE][b]NextTime = 0[/b]
if (CurTime() > NextTime) then
if (client:Health() <= hpm*(1/4))
then
[/Quote]
Right... This should work:
[lua]function alert()
local client = LocalPlayer( )
local hpm = 100
NextTime = NextTime or 0
if ( CurTime( ) > NextTime ) then
if ( client:Health( ) <= hpm / 4 ) then
RunConsoleCommand( "play", "lhalert" )
end
NextTime = CurTime( ) + 1
end
end
hook.Add( "Tick", "alert", alert )[/lua]
[QUOTE=Overv;17119685]Right... This should work:
[lua]function alert()
local client = LocalPlayer( )
local hpm = 100
NextTime = NextTime or 0
if ( CurTime( ) > NextTime ) then
if ( client:Health( ) <= hpm / 4 ) then
RunConsoleCommand( "play", "lhalert" )
end
NextTime = CurTime( ) + 1
end
end
hook.Add( "Tick", "alert", alert )[/lua][/QUOTE]
Gah... No such luck..
-snip-
[QUOTE=Overv;17119756]Oops, replace
[lua]NextTime = NextTime or 0[/lua]
with
[lua]NextTime = NextTime or ( CurTime( ) + 1 )[/lua][/QUOTE]
It didn't work.
[b]EDIT:[/b]
[QUOTE=Dave_Parker;17119775]Or use an infinite timer which runs every second.[/QUOTE]
I've tried a timer.. but it still didn't work.
It's called from my cl_init could that be the reason?
A timer does pretty much the same thing. How does it not work?
[b]Edit:[/b] Wait a sec, 0 was right. Damn you for confusing me. That code should work, you did something wrong yourself.
[lua]
local function Alert( )
if( LocalPlayer( ):Health( ) < 100 / 4 ) then
RunConsoleCommand( "play", "lhalert" );
end
end
timer.Create( "AlertTimer", 1, 0, Alert );
[/lua]
Simple as that. What doesn't work?
[QUOTE=Overv;17119790]A timer does pretty much the same thing. How does it not work?
[b]Edit:[/b] Wait a sec, 0 was right. Damn you for confusing me. That code should work, you did something wrong yourself.[/QUOTE]
Hehe Sorry ^^; But it's still doesn't seem to work.
[quote]What doesn't work? [/quote]
The sound is playing as if the command is being sent every millisecond.
You did something wrong, it worked fine for me when used with lua_openscript_cl. (My code and I'm certain Nevec's works too)
-snip-
Awesome!! Thanks Nevec! You too Overv hehe
I hope you haven't left this thread because:[lua]
RunConsoleCommand( "play", "lhalert" )
[/lua]
[u][b]What.[/b][/u]
Use surface.PlaySound( "..." )
I need to practice my Lua :cawg:
[lua]if( LocalPlayer( ):Health( ) < 100 / 4 ) then[/lua]
[lua]if( LocalPlayer( ):Health( ) < 25 ) then[/lua]
Why make it do the math when you could do it in your head and put it down yourself? It would only make sense doing it that way if you were using a variable that might change or had a really odd number you're too lazy to figure out.
Sorry, you need to Log In to post a reply to this thread.