Problem with stamina simulator: Stamina refreshes by itselfs!

Hi, I’m new to lua programming and I’m working on a gamemode that adds to base gamemode stamina. Basically it’s just a way to limitate sprint duration.
I did not finish it because I’m blocked here.
I think this piece of code just decrease stamina while Speed button is pressed. It works, except that Stamina turns back to its start value after a random time!
Why do this happen?



open_team_menu = function ()
	if LocalPlayer():KeyDown(IN_SPEED) and LocalPlayer():GetNWFloat("Stamina") >= 0.5 then
		timer.Create("Consumo Stamina", 0.1, LocalPlayer():GetNWFloat("Stamina")/0.1, function()
			LocalPlayer():SetNWFloat( "Stamina", LocalPlayer():GetNWFloat("Stamina") - 0.1)
			if LocalPlayer():KeyDown(IN_SPEED)==false then 
				timer.Destroy("Consumo Stamina")
			end
		end)
	end
end

hook.Add("KeyPress","Apri scelta squadra",open_team_menu)


PS: I check Stamina with this:



function GM:HUDPaint()
	draw.SimpleText("Stamina", "Trebuchet24",100,ScrH()-70, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
	draw.SimpleText(LocalPlayer():GetNWFloat("Stamina"), "Trebuchet24",150,ScrH()-70, Color(255,255,255,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
end


Please help me! Thank you!

Don’t use LocalPlayer(), since this is server side. Use ply as a function parameter.

Something like :



open_team_menu = function (ply)
	if ply:KeyDown(IN_SPEED) and ply:GetNWFloat("Stamina") >= 0.5 then
		timer.Create("Consumo Stamina", 0.1, ply:GetNWFloat("Stamina")/0.1, function()
			ply:SetNWFloat( "Stamina", ply:GetNWFloat("Stamina") - 0.1)
			if ply:KeyDown(IN_SPEED)==false then 
				timer.Destroy("Consumo Stamina")
			end
		end)
	end
end

hook.Add("KeyPress","Apri scelta squadra",open_team_menu)


I’d think you’d want to atleast set player’s stamina, for example,



function SetSpawnStam(ply)
	if ( ply:GetNWFloat("Stamina") == nil ) then
		ply:SetNWFloat( "Stamina", 1 )  -- Substitute 1 for whatever starting value.
	end
end

hook.Add( "PlayerSpawn", "SetSpawnStam", SetSpawnStam )


Thx for answer. My code is runned clientside (I thought this way better).
I did your changes and, as I supposed, they did not work.
Same situation: Stamina decreases correctly but with no apparent reason, at any moment, it resets to its start value!

Well, try not to run them client side, it’s silly. Run them serverside, it will be more consistent. There is a lot that can go wrong with clientside stuff. I already made a stamina system recently, and it’s all serverside, give or take the hud bit.

Ok i made it serverside and it works properly, but still I cannot understand why.
Why making stamina consume serverside is more consistent? I thought that was better clientside because each client consumes its stamina when its user push the button.
And still, why when made it clientside stamina was used to self-reset?

It’s just life, I’m not really sure why. Mind you, the KeyPressed shit is mainly for serverside stuff, not for clientside. Also, it’s more consistent server side, because it doesn’t rely on the players CPU speed, or anything like that, it just runs small, low-consuming server-side cpu cycles.

Also, for example, you obviously know about darkRP. DarkRP has a money system, that runs all through the serverside. That’s because it’s easier to manage like that, eg. making meta functions for the players regarding money.

Ok got it.
Thank you very much, you saved my day!

Don’t sweat it, add me on steam if you got any other questions, will gladly help :v: