Hi, i want a variable called ply.stamina to decrease -5 each 0.3 seconds while sprinting (ply.sprinting = true) , and increase +5 each 0.25 seconds while not sprinting (ply.sprinting = false).
How can I do that? Maybe its super easy but i just do not get it. I also want that stamina to be drawn on the player's screen so the number in the screen has to change in real time.
[highlight](User was banned for this post ("Undescriptive and obnoxious thread title" - postal))[/highlight]
This will help
[url]https://wiki.garrysmod.com/page/timer/Create[/url]
For continiously updating it I'd use NWInts since they continiously update
Would this work?
[CODE]hook.Add( "PlayerInitialSpawn", "Datos iniciales", function(ply)
for k, v in pairs( player.GetAll() ) do
v:SetNWInt( 'stamina', 100 )
end
end)
hook.Add("KeyPress" , "PulsarW" , function(ply, key)
if (key == IN_FORWARD) then
if (ply.ultimaW and ply.ultimaW > CurTime() and v:GetNWInt( 'stamina', 0 ) > 0) then
ply.sprint = true
timer.Create( "restar", 0.3, 0, function() v:SetNWInt( 'stamina', 100 ) = v:GetNWInt( 'stamina', 0 ) - 5 )
timer.Remove( "sumar" )
end
ply.ultimaW = CurTime() + 0.3
end
end)
hook.Add("KeyRelease" , "SoltarW" , function(ply,key)
if (key == IN_FORWARD) then
ply.sprint = false
timer.Create( "sumar", 0.3, 0, function() v:SetNWInt( 'stamina', 100 ) = v:GetNWInt( 'stamina', 0 ) + 5 )
timer.Remove( "restar" )
end
end)
hook.Add("TTTPlayerSpeed", "CalcularVelocidad", function(ply, slowed)
local scale = ply.sprint and 1.9 or 1
return scale end)[/CODE]
I think it would work, you can try it and post results.
Oh Jesus don't use timers for this..
Check if ply.ultimaW < CurTime () and if it is, deduct your stamina and define it again as Curtime () + 0.3
But how would that decrease the stamina each 0.3 seconds?
It checks to see if 0.3 seconds have passed, if it has it'll deduct the amount and set the next check 0.3 seconds later
It checks when player presses w.
You basically have it right in your code just don't use timers. If a player dies running or worse, the timer is still going to operate
My code can't decrease the stamina :( i can sprint for ever
Looking closer at your code there's a lot wrong with the way you're thinking
First of all your initialspawn hook resets EVERYONE's stamina when someone joins. You don't even need to set anything because NWInts allow for default values in the first place, so that's useless. You should be setting an index if they're sprinting to false there. I guess ply.sprint is yours.
Second of all your hooks are completely fucked. You're checking to see if the ply.ultimaW > CurTime (), which it always will be considering you keep defining jt as such. Also you're checking to see if the variable exists which is useless too, because it'll never define it until it exists.
Third of all your timer logic are fucked too. DONT use timers for this, but recognize you're not using them right in the first place. You're creating them as infinite repeating timers and then destroying them. Just create them as single repeating timers.
[editline]6th December 2015[/editline]
Finally for your issue, consider creating a player think function and running it on all players through the SERVER think function. That's the best way
[editline]6th December 2015[/editline]
Within that player think function you can check if they're running and deduct stamina accordingly
Sorry, you need to Log In to post a reply to this thread.