• Stamina
    5 replies, posted
I'm am using TCB Stamina mod, and I was wondering how can i make it so when a players stamina is 0 and they try to run or jump a message comes up using the DarkRP.notify function?
I never used it or seen the code but I think the way that should be done is doing something like. [code] //Replace <Stamina> with the way of getting the current stamina if(LocalPlayer:<Stamina> <= 0) { //Do this } [/code]
[code] /*--------------------------------------------------------------------------- Creator: TheCodingBeast - TheCodingBeast.com This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/ ---------------------------------------------------------------------------*/ local ShouldDrawHUD = true -- true/false local DefaultRunSpeed = 240 -- If you change this value you need to change it in garrysmod\addons\darkrpmodification\lua\darkrp_config\settings.lua too local DefaultWalkSpeed = 160 -- If you change this value you need to change it in garrysmod\addons\darkrpmodification\lua\darkrp_config\settings.lua too local DefaultJumpPower = 200 local DisableLevel = 10 -- (0 - 100) When should Run & Jump get disabled local StaminaDrainSpeed = 0.25 -- Time in seconds local StaminaRestoreSpeed = 0.75 -- Time in seconds -- Server if (SERVER) then -- PlayerSpawn function tcb_StaminaStart( ply ) timer.Destroy( "tcb_StaminaTimer" ) ply:SetRunSpeed( DefaultRunSpeed ) ply:SetNWInt( "tcb_Stamina", 100 ) tcb_StaminaRestore( ply ) end hook.Add( "PlayerSpawn", "tcb_StaminaStart", tcb_StaminaStart ) -- KeyPress function tcb_StaminaPress( ply, key ) if key == IN_SPEED or ply:KeyDown(IN_SPEED) then if ply:GetNWInt( "tcb_Stamina" ) >= DisableLevel then ply:SetRunSpeed( DefaultRunSpeed ) timer.Destroy( "tcb_StaminaGain" ) timer.Create( "tcb_StaminaTimer", StaminaDrainSpeed, 0, function( ) if ply:GetNWInt( "tcb_Stamina" ) <= 0 then ply:SetRunSpeed( DefaultWalkSpeed ) timer.Destroy( "tcb_StaminaTimer" ) return false end ply:SetNWInt( "tcb_Stamina", ply:GetNWInt( "tcb_Stamina" ) - 1 ) end) else ply:SetRunSpeed( DefaultWalkSpeed ) timer.Destroy( "tcb_StaminaTimer" ) end end if key == IN_JUMP or ply:KeyDown(IN_JUMP) then if ply:GetNWInt( "tcb_Stamina" ) >= DisableLevel then ply:SetJumpPower( DefaultJumpPower ) ply:SetNWInt( "tcb_Stamina", ply:GetNWInt( "tcb_Stamina" ) - 1 ) else ply:SetJumpPower( 0 ) end end end hook.Add( "KeyPress", "tcb_StaminaPress", tcb_StaminaPress ) -- KeyRelease function tcb_StaminaRelease( ply, key ) if key == IN_SPEED and !ply:KeyDown(IN_SPEED) then timer.Destroy( "tcb_StaminaTimer" ) tcb_StaminaRestore( ply ) end end hook.Add( "KeyRelease", "tcb_StaminaRelease", tcb_StaminaRelease ) -- StaminaRestore function tcb_StaminaRestore( ply ) timer.Create( "tcb_StaminaGain", StaminaRestoreSpeed, 0, function( ) if ply:GetNWInt( "tcb_Stamina" ) >= 100 then return false else ply:SetNWInt( "tcb_Stamina", ply:GetNWInt( "tcb_Stamina" ) + 1 ) end end) end end -- Client if (CLIENT) then -- HUDPaint function tcb_StaminaDraw( ) if ShouldDrawHUD == true then if LocalPlayer():GetNWInt( "tcb_Stamina" ) <= DisableLevel then StaminaDrawColor = Color( 255, 0, 0, 255) else StaminaDrawColor = Color( 255, 255, 255, 255) end draw.DrawText( "Stamina: "..LocalPlayer():GetNWInt( "tcb_Stamina" ), "TargetID", 11, 11, Color(0, 0, 0, 255) ) draw.DrawText( "Stamina: "..LocalPlayer():GetNWInt( "tcb_Stamina" ), "TargetID", 10, 10, StaminaDrawColor ) end end hook.Add( "HUDPaint", "tcb_StaminaDraw", tcb_StaminaDraw ) [/code] that is the code
Not the most ideal method... I was teaching someone how to write and the lesson ended here: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_stamina_system/sh_stamina_system.lua[/url] [ NOTE ] Remove .html to view / copy .Lua ... The final versions are: ... basic: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_stamina_system/sh_basic_stamina_system.lua.html[/url] Extended: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_stamina_system/sh_extended_stamina_system.lua.html[/url] Basic only has the system, some CONST vars, and some networking to reset on death / respawn. The extended has all of the normal helper functions you'd expect in addition to the below; it makes the code friendlier to read. It also has a lot of configuration / settings in the form of CONSTs: [code]// At what rate per second stamina depletes when at max-speed? local STAMINA_RATE_DEPLETE = -10; // At what rate per second stamina regenerates when standing still? local STAMINA_RATE_REGEN = 10; // Default maximum stamina local STAMINA_DEFAULT_MAX = 100; // How much stamina should we lose per hp lost? false to disable local STAMINA_DEPLETE_PER_DAMAGE = 0.10; // How much time the player must wait after losing stamina before being able to regen, false to disable local STAMINA_REGEN_DELAY = 2; // Should the regen delay also occur when simply walking? local STAMINA_REGEN_DELAY_ONMOVE = false; // At what value stamina should the player start slowing? false to disable local STAMINA_HANDICAP_START = 25; // If a player should be slowed down after taking damage ( false to disable otherwise time in seconds ) local STAMINA_DAMAGE_HANDICAP_TIME = 1.5; // If a player should be slowed down when landing on the ground ( false to disable otherwise time in seconds ) local STAMINA_FALL_HANDICAP_TIME = 1.5; // How much stamina should the player lose when jumping? false to disable local STAMINA_JUMP_COST = -10; // At what level of stamina is jump blocked from use? false to disable local STAMINA_JUMP_HANDICAP_START = 25; [/code] these should handle everything the code you provided does, and more. It also is more optimized because it only needs to network data when the player respawns / dies when stamina resets, otherwise it's handled independently by client and server in shared hook which means both client and server will stay in sync properly. It doesn't use timers ( which break quite frequently now-a-days ) and it properly removes stamina based on run-speed instead of keypresses ( so there is no GTA style spam shift to run forever type exploits ) and it also adds new hooks ( PlayerJumped ) so you can easily use hook.Add( "PlayerJumped", "NotifyIfCan'tJump", function( _p ) if ( STAMINA_JUMP_HANDICAP_START && _p:Stamina( ) <= STAMINA_JUMP_HANDICAP_START ) then _p:Notify( "You can't jump!" ); end end ); It's also free to use or modify as you please; just don't try selling it.
Wouldn't it be better to be able to return true or false in that hook for jumping? Now that I think about it, it depends on the situation. Meh, I guess it's something I would have, but maybe others wouldn't care about it. I think I just like having that option easily there. Oh well, I'm not using this, just making a suggestion.
[QUOTE=AnonTakesOver;47449802]Wouldn't it be better to be able to return true or false in that hook for jumping? Now that I think about it, it depends on the situation. Meh, I guess it's something I would have, but maybe others wouldn't care about it. I think I just like having that option easily there. Oh well, I'm not using this, just making a suggestion.[/QUOTE] I suppose it would make sense if I added another hook like I did for PlayerDroppedWeapon and CanPlayerDropWeapon using _p:DropWeapon( _w ); so that it'd make it easier to prevent jumping, etc.. Or do you mean to prevent the hook from running anything else if they can't jump? Either way, I do agree that I should change a bit of the code around because it may be possible to have jump disabled and it'll still fire the hook in this example...
Sorry, you need to Log In to post a reply to this thread.