New Hud Stamina Bar Not Changing.

The title says it all, but I’ve made a new hud with a stamina bar, but no matter what I try it will not deplete, in a last ditch effort I tried it with this code and it’s still not going down.

cl_hud.lua
[lua]function newhud()

local playerdata = util.KeyValuesToTable(file.Read("Dusted/Saves/"..string.gsub(LocalPlayer():SteamID(), ":", "_")..".txt"))
local playerstamina = playerdata["lvl"]["stamina"]
local health = LocalPlayer():Health()
local stamina = 100
timer.Create( "UpdateStamina", 600, 0, function()
	playerdata = util.KeyValuesToTable(file.Read("Dusted/Saves/"..string.gsub(LocalPlayer():SteamID(), ":", "_")..".txt"))
	playerstamina = playerdata["lvl"]["stamina"]
end )
timer.Create( "Test", 4, 0, function()
	stamina = stamina - 1
end)
timer.Start( "Test" )
if( LocalPlayer():KeyReleased( IN_RUN ) ) then
end

if( LocalPlayer():KeyPressed( IN_RUN ) ) then
end

draw.RoundedBox( 0, 5, ScrH() - 15 - 20, health * 2.5, 15, Color( 255, 0, 0, 255 ) )
draw.SimpleText( health .. "% Health", "default", 10, ScrH() - 15 - 40, Color( 255, 255, 255, 255 ) )

draw.RoundedBox( 0, 5, ScrH() - 15 - 60, stamina * 2.5, 15, Color( 0, 0, 255, 255 ) )
draw.SimpleText( stamina .. "% Stamina", "default", 10, ScrH() - 15 - 80, Color( 255, 255, 255, 255) )

end
hook.Add( “HUDPaint”, “MyHudName”, newhud )[/lua]

That’s not how hooks are used, it needs to be more like this:
[lua]
local Stamina = 100
local Running = false
local nextStaminaDrop = CurTime()
local playerdata = {}
local playerstamina = 0

function newhud()

-- Only place drawing functions here
local health = LocalPlayer():Health()
draw.RoundedBox( 0, 5, ScrH() - 15 - 20, health * 2.5, 15, Color( 255, 0, 0, 255 ) )
draw.SimpleText( health .. "% Health", "default", 10, ScrH() - 15 - 40, Color( 255, 255, 255, 255 ) )
 
draw.RoundedBox( 0, 5, ScrH() - 15 - 60, stamina * 2.5, 15, Color( 0, 0, 255, 255 ) )
draw.SimpleText( stamina .. "% Stamina", "default", 10, ScrH() - 15 - 80, Color( 255, 255, 255, 255) )

end
hook.Add( “HUDPaint”, “MyHudName”, newhud )

– Hook that is called when player presses a key
function staminaKeyPress( ply, key)
if key == IN_RUN then
Running = true
end
end
hook.Add(“KeyPress”, “StaminaKeyPress”, staminaKeyPress)

– Hook that is called when player releases a key
function staminaKeyRelease( ply, key)
if key == IN_RUN then
Running = false
end
end
hook.Add(“KeyRelease”, “StaminaKeyRelease”, staminaKeyRelease)

– This function takes care of making the stamina go up or down
– Will also load data if it’s not loaded
function DoStamina()

if !playerdata then -- if player data is nil then load the data
	    playerdata = util.KeyValuesToTable(file.Read("Dusted/Saves/"..string.gsub(LocalPlayer():SteamID(), ":", "_")..".txt"))
		playerstamina = playerdata["lvl"]["stamina"]
		Stamina = playerstamina
end

if CurTime() > nextStamina then 
	if Running then
		Stamina = Stamina - 1
	else
		Stamina = Stamina + 1
	end
	
	Stamina = math.Clamp(Stamina, 0,playerstamina)
	nextStamina = CurTime() + 4
end

end
hook.Add( “Think”, “StaminaThink”, DoStamina)

– Timer shouldn’t go in the HUDPaint It will be restarted constantly
timer.Create( “UpdateStamina”, 600, 0, function()
playerdata = util.KeyValuesToTable(file.Read(“Dusted/Saves/”…string.gsub(LocalPlayer():SteamID(), “:”, “_”)…".txt"))
playerstamina = playerdata[“lvl”][“stamina”]
Stamina = playerstamina
end )
[/lua]
This is an untested modification of the code you posted, but it may work as is, if you have more questions post’em and i’ll try and explain.

I did a little improv because I had some other plans for it but it’s still not working out for me here. The staminaUp was added in there so that it would have a different amount of time it took instead of going down. The stamina is just not changing though, everything else is working fine.

[lua]local playerdata = {}
local playerstamina = 0
local nextStaminaDrop = CurTime()
local stamina = 100
local nextStaminaUp = CurTime()
local Running = false

function newhud()
local health = LocalPlayer():Health()

draw.RoundedBox( 0, 5, ScrH() - 15 - 20, health * 2.5, 15, Color( 255, 0, 0, 255 ) )
draw.SimpleText( health .. "% Health", "default", 10, ScrH() - 15 - 40, Color( 255, 255, 255, 255 ) )

draw.RoundedBox( 0, 5, ScrH() - 15 - 60, stamina * 2.5, 15, Color( 0, 0, 255, 255 ) )
draw.SimpleText( stamina .. "% Stamina", "default", 10, ScrH() - 15 - 80, Color( 255, 255, 255, 255) )

end
hook.Add( “HUDPaint”, “MyHudName”, newhud )

– Hook is called when player presses a key
function StaminaKeyPress( ply, key )
if key == IN_RUN then
Running = true
end
end
hook.Add( “KeyPress”, “StaminaKeyPress”, StaminaKeyPress )

– Hook is called when the player releases a key
function StaminaKeyRelease( ply, key )
if key == IN_RUN then
Running = false
end
end
hook.Add( “KeyRelease”, “StaminaKeyRelease”, StaminaKeyRelease )

function DoStamina()
if !playerdata then – If the playerdata is nil then load the data
playerdate = util.KeyValuesToTable(“Dusted/Saves/”…string.gsub(LocalPlayer():SteamID(), “:”, “_”)…".txt" )
playerstamina = playerdata[“lvl”][“stamina”]
end
if Running then
if CurTime() > nextStaminaDrop then
if stamina > 1 then
Stamina = stamina - 1
end
end
else
if CurTime() > nextStaminaUp then
if stamina < 100 then
stamina = stamina + 1
end
end
nextStaminaDrop = CurTime() + (.1 * playerstamina)
nextStaminaUp = CurTime() + 1
end
end
hook.Add( “Think”, “StaminaThink”, DoStamina )

timer.Create( “UpdateStamina”, 600, 0, function()
playerdata = util.KeyValuesToTable(file.Read(“Dusted/Saves/”…string.gsub(LocalPlayer():SteamID(), “:”, “_”)…".txt"))
playerstamina = playerdata[“lvl”][“stamina”]
end )[/lua]

looks like I got some mixed case with the stamina variables on line 43 it should be stamina = stamina - 1
right now it’s Stamina = stamina - 1 , variables are case sensitive, so Stamina, STamina, and stamina are all different variables. might want to check the other variables for case issues too.

I fixed up the stamina variables so they were the same case but it’s still not changing the stamina level at all… Do I need to setup a meta:function() to do the work for me? I hate setting those up XD

line 37 says playerdate instead of playerdata

I think that’s all the typos I see so far, also double check the console for errors but I think that should fix it.

Ok I tried yet again and it still is giving me issues, the stamina will not change for anything… No matter how hard it tries at it. XD

Mise will just post the code and give you a chance to see, maybe you can kick it apart for me.

cl_hud.lua
[lua]local playerdata = {}
local playerstamina = 0
local nextStaminaDrop = CurTime()
local stamina = 100
local nextStaminaUp = CurTime()
local Running = false

function newhud()
local health = LocalPlayer():Health()

draw.RoundedBox( 0, 5, ScrH() - 15 - 20, health * 2.5, 15, Color( 255, 0, 0, 255 ) )
draw.SimpleText( health .. "% Health", "default", 10, ScrH() - 15 - 40, Color( 255, 255, 255, 255 ) )

draw.RoundedBox( 0, 5, ScrH() - 15 - 60, stamina * 2.5, 15, Color( 0, 0, 255, 255 ) )
draw.SimpleText( stamina .. "% stamina", "default", 10, ScrH() - 15 - 80, Color( 255, 255, 255, 255) )

end
hook.Add( “HUDPaint”, “MyHudName”, newhud )

– Hook is called when player presses a key
function StaminaKeyPress( ply, key )
if key == IN_RUN then
Running = true
end
end
hook.Add( “KeyPress”, “StaminaKeyPress”, StaminaKeyPress )

– Hook is called when the player releases a key
function StaminaKeyRelease( ply, key )
if key == IN_RUN then
Running = false
end
end
hook.Add( “KeyRelease”, “StaminaKeyRelease”, StaminaKeyRelease )

function DoStamina()
if !playerdata then – If the playerdata is nil then load the data
playerdata = util.KeyValuesToTable(“Dusted/Saves/”…string.gsub(LocalPlayer():SteamID(), “:”, “_”)…".txt" )
playerstamina = playerdata[“lvl”][“stamina”]
end
if Running then
if CurTime() > nextStaminaDrop then
if stamina > 1 then
stamina = stamina - 1
end
end
else
if CurTime() > nextStaminaUp then
if stamina < 100 then
stamina = stamina + 1
end
end
nextStaminaDrop = CurTime() + (.1 * playerstamina)
nextStaminaUp = CurTime() + 1
end
end
hook.Add( “Think”, “StaminaThink”, DoStamina )

timer.Create( “UpdateStamina”, 600, 0, function()
playerdata = util.KeyValuesToTable(file.Read(“Dusted/Saves/”…string.gsub(LocalPlayer():SteamID(), “:”, “_”)…".txt"))
playerstamina = playerdata[“lvl”][“stamina”]
end )

function hidehud( name )
for k, v in pairs( { “CHudHealth”, “CHudBatter”} ) do
if name == v then
return false
end
end
end
hook.Add( “HUDShouldDraw”, “HideOurHud”, hidehud )[/lua]

Try this, re-wrote the variable name so I could think easier feel free to change them back if it works, also I simplified some of the logic, after getting a little over halfway done with the code I got to thinking it’s probably where you were setting the next time to update, but here’s the code:
[lua]
local plyData = {}
local plyStamina = 0
local plyMaxStamina = 0
local plyRunning = false

local nextStaminaDrop = CurTime()
local nextStaminaUp = CurTime()

function newhud()

-- Got rid of health and moved it to the SimpleText Function don't really need to make a variable.
 
draw.RoundedBox( 0, 5, ScrH() - 15 - 20, health * 2.5, 15, Color( 255, 0, 0, 255 ) )
draw.SimpleText( LocalPlayer():Health() .. "% Health", "default", 10, ScrH() - 15 - 40, Color( 255, 255, 255, 255 ) )
 
draw.RoundedBox( 0, 5, ScrH() - 15 - 60, stamina * 2.5, 15, Color( 0, 0, 255, 255 ) )
draw.SimpleText( stamina .. "% stamina", "default", 10, ScrH() - 15 - 80, Color( 255, 255, 255, 255) )

end
hook.Add( “HUDPaint”, “MyHudName”, newhud )

– Hook is called when player presses a key
function StaminaKeyPress( ply, key )
if key == IN_RUN then
plyRunning = true
end
end
hook.Add( “KeyPress”, “StaminaKeyPress”, StaminaKeyPress )

– Hook is called when the player releases a key
function StaminaKeyRelease( ply, key )
if key == IN_RUN then
plyRunning = false
end
end
hook.Add( “KeyRelease”, “StaminaKeyRelease”, StaminaKeyRelease )

function DoStamina()
if !plyData then
LoadPlayerData()
end
if plyRunning and (CurTime() > nextStaminaDrop) then
plyStamina = plyStamina - 1
if plyStamina < 1 then plyStamina = 1 end
nextStaminaDrop = CurTime() + (.1 * plyMaxStamina)
end
if !plyRunning and(CurTime() > nextStaminaUp) then
plyStamina = plyStamina + 1
if plyStamina > plyMaxStamina then plyStamina = plyMaxStamina end
nextStaminaUp = CurTime() + 1
end
end
hook.Add( “Think”, “StaminaThink”, DoStamina )

function LoadPlayerData()
plyData = util.KeyValuesToTable(file.Read(“Dusted/Saves/”…string.gsub(LocalPlayer():SteamID(), “:”, “_”)…".txt"))
print("plyData is " … tostring(plyData)) – Check the console for this line, if it’s nil the data isn’t loading right
plyMaxStamina = plyData[“lvl”][“stamina”]
print("plyMaxStamina is " … tostring(plyMaxStamina)) – Same here
plyStamina = plyMaxStamina
end

function hidehud( name )
for k, v in pairs( { “CHudHealth”, “CHudBatter”} ) do
if name == v then
return false
end
end
end
hook.Add( “HUDShouldDraw”, “HideOurHud”, hidehud )

–I don’t know why you are reloading the player’s data every 10 min but uncommnet the next line if really need to
–timer.Create( “UpdateStamina”, 600, 0, LoadPlayerData)
[/lua]

Yeah it didn’t print off any message for me…

Just a quick note, the playerstamina is actually a level, so it is used to time how long the person has before they’re out of stamina, it is not the max amount of stamina that a person has.

I changed the code so it would spam my console with the stuff if it worked and low and behold it didn’t, so somewhere in the first 33 lines I have an error. There is an error that says something along the lines of D3D Error. No idea what that is, is it have trouble drawing the boxes or something?

This is not based off of sandbox, it is a base gamemode so I do not know if one of those crosses the line with it. =P

[lua]local playerdata = {}
local playerstamina = 0
local nextStaminaDrop = CurTime()
local stamina = 100
local nextStaminaUp = CurTime()
local Running = false

function newhud()
local health = LocalPlayer():Health()

draw.RoundedBox( 0, 5, ScrH() - 15 - 20, health * 2.5, 15, Color( 255, 0, 0, 255 ) )
draw.SimpleText( health .. "% Health", "default", 10, ScrH() - 15 - 40, Color( 255, 255, 255, 255 ) )

draw.RoundedBox( 0, 5, ScrH() - 15 - 60, stamina * 2.5, 15, Color( 0, 0, 255, 255 ) )
draw.SimpleText( stamina .. "% stamina", "default", 10, ScrH() - 15 - 80, Color( 255, 255, 255, 255) )

end
hook.Add( “HUDPaint”, “MyHudName”, newhud )

– Hook is called when player presses a key
function StaminaKeyPress( ply, key )
if key == IN_RUN then
Running = true
end
end
hook.Add( “KeyPress”, “StaminaKeyPress”, StaminaKeyPress )

– Hook is called when the player releases a key
function StaminaKeyRelease( ply, key )
if key == IN_RUN then
Running = false
end
end
hook.Add( “KeyRelease”, “StaminaKeyRelease”, StaminaKeyRelease )

function DoStamina()
playerdata = util.KeyValuesToTable(file.Read(“Dusted/Saves/”…string.gsub(LocalPlayer():SteamID(), “:”, “_”)…".txt"))
playerstamina = playerdata[“lvl”][“stamina”]
print("plyMaxStamina is " … tostring(playerstamina)) – Same here
if Running and ( CurTime() > nextStaminaDrop ) then
if stamina < 1 then
– nothing
else
stamina = stamina - 1
end
nextStaminaDrop = CurTime() + ( .1 * playerstamina )
end
if !Running and ( CurTime() > nextStaminaUp ) then
if stamina > 99 then
– nothing
else
stamina = stamina + 1
end
nextStaminaUp = CurTime() + 1
end
end
hook.Add( “Think”, “StaminaThink”, DoStamina )[/lua]

Can you copy and paste the error?

Actually upon closer inspection that is no longer the issue. (Console was hiding them)

But! The stamina bar is still not chaning and the playerstamina is set to 4. >_> .1 * 4 = .4 if I remember correctly so every .4 seconds… Wait, does current time do .4?

Should work just fine, CurTime() returns a decimal.
Take the local off of stamina so
[lua]
stamina = 100
– and not
local stamina = 100
[/lua]
then go to the console and try this:



lua_run_cl stamina = 30


See if the Hud changes

EDIT:
Also this only checks if the run key is down, not if the player is moving so after getting it working you’ll want to add a check that LocalPlayer:GetVelocity():Length() > 0

The other way to go is track the stamina on the server side and then send a usermessage every time stamina changes to each player, if we can’t get this working we can try doing it that way.

Well it’s counting upwards, so it seems the shift key is not registering.

odd, try placing a print above the if in KeyPress to see if it’s being called at all. on the wiki it says it’s a shared hook, so might want to try it on the server with usermessages to update the info.

Haha, alright I got it working beautifully now, I’ll input the rest of my code later (tis a secret concoction of witchery and brewery, but for those who are wondering what it looks like, here ya go)

[lua]local playerdata = nil
local playerstamina = 0
local nextStaminaDrop = CurTime()
stamina = 100
local nextStaminaUp = CurTime()
local Running = false

function newhud()
local health = LocalPlayer():Health()

draw.RoundedBox( 0, 5, ScrH() - 15 - 20, health * 2.5, 15, Color( 255, 0, 0, 255 ) )
draw.SimpleText( health .. "% Health", "default", 10, ScrH() - 15 - 40, Color( 255, 255, 255, 255 ) )

draw.RoundedBox( 0, 5, ScrH() - 15 - 60, stamina * 2.5, 15, Color( 0, 0, 255, 255 ) )
draw.SimpleText( stamina .. "% stamina", "default", 10, ScrH() - 15 - 80, Color( 255, 255, 255, 255) )

end
hook.Add( “HUDPaint”, “MyHudName”, newhud )

– Hook is called when player presses a key
function StaminaKeyPress( ply, key )
– Developer line, undash to see
– LocalPlayer():ChatPrint( LocalPlayer():Nick() … " pressed the " … key … " key." )
if key == IN_SPEED then
Running = true
end
end
hook.Add( “KeyPress”, “StaminaKeyPress”, StaminaKeyPress )

– Hook is called when the player releases a key
function StaminaKeyRelease( ply, key )
if key == IN_SPEED then
Running = false
end
end
hook.Add( “KeyRelease”, “StaminaKeyRelease”, StaminaKeyRelease )

function DoStamina()
if !playerdata then
playerdata = util.KeyValuesToTable(file.Read(“Dusted/Saves/”…string.gsub(LocalPlayer():SteamID(), “:”, “_”)…".txt"))
playerstamina = playerdata[“lvl”][“stamina”]
end
if Running and ( CurTime() > nextStaminaDrop ) then
if stamina < 1 then
– nothing
else
stamina = stamina - 1
end
nextStaminaDrop = CurTime() + (.01 * playerstamina)
end
if !Running and ( CurTime() > nextStaminaUp ) then
if stamina > 99 then
– nothing
else
stamina = stamina + 1
end
nextStaminaUp = CurTime() + 1
end
end
hook.Add( “Think”, “StaminaThink”, DoStamina )

function hidehud( name )
for k, v in pairs( { “CHudHealth”, “CHudBatter”} ) do
if name == v then
return false
end
end
end
hook.Add( “HUDShouldDraw”, “HideOurHud”, hidehud )[/lua]

I see, the IN_ was wrong I thought it was IN_RUN, guess it was IN_SPEED. Good job finding that.

Hahaha yeah that was a nasty one. Thanks for all the help!