• Stamina Script Help
    8 replies, posted
Hi, I am new to lua and I am having trouble with a stamina script I am trying to make. It is very simple but I don't understand something and was hoping that someone could explain it to me. Basically, the actual stamina amount (ply:GetNWInt("stamina")) is out of sync with the HUD stamina bar and percentage which are being displayed on the clients screen. I thought it was something to do with the Think hook but I really have no idea. If someone could explain this to me I would greatly appreciate it. [lua] if (CLIENT) then function stamina_hud() local ply = LocalPlayer() if not ply:Alive() then return end if (ply:GetActiveWeapon() == NULL or ply:GetActiveWeapon() == "Camera") then return end draw.RoundedBox(3, 3, 3, 204, 34, Color(0, 0, 0, 255)) draw.RoundedBox(3, 5, 5, ply:GetNWInt("stamina")*2, 30, Color(100, 200, 255, 255)) draw.RoundedBox(3, 5, 20, ply:GetNWInt("stamina")*2, 15, Color(100, 210, 255, 255)) draw.SimpleText(math.Round(ply:GetNWInt("stamina")) .. "%", "Default", 95, 15, Color(86, 104, 86, 255), 0, 0) end hook.Add("HUDPaint", "stamina_hud", stamina_hud) end if (SERVER) then function stamina_spawn( ply ) ply:SetNWInt( "stamina", 100 ) ply:SetNWInt( "rs", ply:GetRunSpeed() ) ply:SetNWInt( "ws", ply:GetWalkSpeed() ) end hook.Add( "PlayerSpawn", "stamina_spawn", stamina_spawn) function stamina_think() for k, ply in pairs( player.GetAll() ) do if(ply:KeyDown(IN_SPEED)) then if(ply:GetNWInt("stamina")>0) then ply:SetNWInt("stamina", ply:GetNWInt("stamina") - 0.1) end else if(ply:GetNWInt("stamina")<100) then ply:SetNWInt("stamina", ply:GetNWInt("stamina") + 0.04) end end if ply:GetNWInt( "stamina" ) < 0 then ply:SetNWInt( "stamina", 0 ) end if ply:GetNWInt( "stamina" ) > 100 then ply:SetNWInt( "stamina", 100 ) end if (ply:GetNWInt( "stamina" ) < 1) then ply:SetRunSpeed( ply:GetNWInt( "ws" ) ) end if (ply:GetNWInt( "stamina" ) > 10 and ply:GetNWInt( "stamina" ) < 11) then ply:SetRunSpeed( ply:GetNWInt( "rs" ) ) end end end hook.Add("Think", "stamina_think", stamina_think) end [/lua]
I just added this to my lua/autorun then added a console command [LUA]local function shows(ply) ply:ChatPrint(ply:GetNWInt("stamina")) end concommand.Add("stam", shows)[/LUA] and it was in sync with the bar. Are you sure it's out of sync for you?
[QUOTE=Loki611;41660566]I just added this to my lua/autorun then added a console command [LUA]local function shows(ply) ply:ChatPrint(ply:GetNWInt("stamina")) end concommand.Add("stam", shows)[/LUA] and it was in sync with the bar. Are you sure it's out of sync for you?[/QUOTE] Pretty sure, I had it output the value into chat inside the server function and it was out of sync, I could tell because my run speed was not changed until the chat number had reached zero even though the percentage had. Edit: Just tested it again and it was definitely not in sync.
On the hud, you're multiplying it by 2. The best thing to do with percentages is turn the value into a float from 0 to 1. So you'd do something like: [lua]// To turn the stamina into a 0-1 value: _percent = CURRENT_STAMINA / MAX_STAMINA // For the hud bar; this will ensure that the bar can go to nothing, and also fill up but no more. MAX_WIDTH_OF_STAMINA_BAR * _percent [/lua] Also make sure that you're setting the variable in the right environment ( client/shared/server )
[QUOTE=Acecool;41660881]On the hud, you're multiplying it by 2. The best thing to do with percentages is turn the value into a float from 0 to 1. So you'd do something like: [lua]// To turn the stamina into a 0-1 value: _percent = CURRENT_STAMINA / MAX_STAMINA // For the hud bar; this will ensure that the bar can go to nothing, and also fill up but no more. MAX_WIDTH_OF_STAMINA_BAR * _percent [/lua] Also make sure that you're setting the variable in the right environment ( client/shared/server )[/QUOTE] I'm not sure if I am missing something but I don't see how that solves it. I tried this but the display is still out of sync with the real values. Is the refresh rate behind?
Excuse me if im being a total fruitcake here, but, im having the hunch that the functions you use are depreciated, instead you may wanna look at these: [url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url] key difference is that with the above you can actually broadcast/listen, instead of just hope that things are synced.
[QUOTE=Suicidal_B;41663816]Excuse me if im being a total fruitcake here, but, im having the hunch that the functions you use are depreciated, instead you may wanna look at these: [url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url] key difference is that with the above you can actually broadcast/listen, instead of just hope that things are synced.[/QUOTE] Tried sending the value from server to client and using it but had no luck. Thanks for the suggestion though.
Ok bummer :(
Actually, take a look at this thread: [url]http://facepunch.com/showthread.php?t=1253834[/url] More specifically, this post, by Garry: [url]http://facepunch.com/showthread.php?t=1253834&p=39919712&viewfull=1#post39919712[/url] He recommends using NetworkVars even if it means creating a special entity to hold those vars: [url]http://wiki.garrysmod.com/page/Networking_Entities[/url] I personally made my own net system which handles all types of data and automatically sends it to the correct function on the client/server-side. It keeps vars in sync, and can send unlimited data using packets ( Just finished this system last week! ). I had 500 cars in the server, worth about 33kb in data ( all emergency lights, all lights on, horns blaring, all sirens on, all had max attachments, owner and more info stored ), then I stored a bunch of spam to the tune of 1mb. Rejoined the server and everything synced flawlessly. It just depends on how you design it. For your system, I wouldn't recommend storing run speed and walk speed ( Taking up space and syncing data which doesn't need to be synced / stored ); those should be the same on the client, and on the server via the function calls. Only store the stamina amount. If you dynamically change the run-speed to match the walk speed based on stamina, have that a shared function based on stamina - with proper sync there will be no issue; It's how I do it.
Sorry, you need to Log In to post a reply to this thread.