• Client Side Spawn Hooks
    7 replies, posted
Hi guys, i'm making a new gamemod and i've been stuck for a while in finding a way to detect when your spawn in a client side way actually this is the code that need to be called when you spawn : [CODE] function Stamina_Spawn() LocalPlayer():SetNWInt( "Stamina", staminaMax ) LocalPlayer():SetNWFloat("LastStamina" , CurTime()) LocalPlayer():SetNWBool("jumpOver", true) stamina_key() --key handler P:PrintMessage( HUD_PRINTTALK, "spawned") end hook.Add( "GetHandsModel", "Stamina_Spawn_client", Stamina_Spawn )[/CODE] i used player_info game listener too but it seems it has been called multiple times so it doesn't fit my needs. i don't know why it doesn't trigger when you spawn. Maybe it's due to my player doesn't get hands model (for a strange reason too) i give you my player override class too in case it's due to the player not getting his hands model and so not triggering the event : [CODE]DEFINE_BASECLASS( "player_default" ) local PLAYER = {} -- -- See gamemodes/base/player_class/player_default.lua for all overridable variables -- PLAYER.WalkSpeed = 200 PLAYER.JumpPower = 400 PLAYER.RunSpeed = 400 function PLAYER:Loadout() self.Player:RemoveAllAmmo() self.Player:Give( "weapon_pistol" ) self.Player:GiveAmmo( 256, "Pistol", true ) self.Player:SwitchToDefaultWeapon() end player_manager.RegisterClass( "player_custom", PLAYER, "player_default" ) function GM:PlayerSpawn( ply ) player_manager.SetPlayerClass( ply, "player_custom" ) ply:SetArmor( 100 ) MsgN( ply:Nick() .. " has spawned!" ) end[/CODE] i've been looking for like 4 hours in garry's wiki and other website and still no clue to resolv this problem P.S : I'm french, sorry for my poor english skill.
Do you want to trigger something on the client when they're fully in the server, or each time the player spawns? You could send a net-message to the client from the server in PlayerSpawn. What is the client supposed to execute? Networked vars will work on the client if you set them on the server ( but they're continuously refreshed each time the client calls Get*... ).
the client is supposed to execute my keypress / keyrelease handler and i need it to be on client because it use infinite timer for each client (so cannot use timer in server side because timer will be shared between player. Here is the code of the handler in case you need it : [CODE] function stamina_key() function KeyPressed (P, key) local sprint = (key == IN_SPEED) and true or false local jump = (key == IN_JUMP) and true or false P:PrintMessage( HUD_PRINTTALK, "key pressed") if(P:IsDrivingEntity())then -- driving mechanics print("is driving") else -- walking mechanic P:PrintMessage( HUD_PRINTTALK, "walking") if(sprint and P:GetNWInt("IsMoving")) then P:PrintMessage( HUD_PRINTTALK, "sprinting") StaminaLoss(P, SprintPenalty) if(!timer.Exists("StaminaSprint")) then timer.Create("StaminaSprint", SprintRate, 0, function() StaminaLoss(P, SprintPenalty) end) end end if(jump)then P:PrintMessage( HUD_PRINTTALK, "jumping") if(P:GetNWBool("jumpOver") and P:GetNWInt("Stamina") >= JumpPenalty)then P:SetNWBool("jumpOver", false) StaminaLoss(P, JumpPenalty) end end end end function KeyReleased (P, key) local sprint = (key == IN_SPEED) and true or false local jump = (key == IN_JUMP) and true or false if(P:IsDrivingEntity())then -- driving mechanics print("is driving") else -- walking mechanic if(sprint) then if(timer.Exists("StaminaSprint")) then timer.Destroy("StaminaSprint") end end end end hook.Add("KeyRelease", "KeyReleasedStamina", KeyReleased) hook.Add("KeyPress", "KeyPressedStamina", KeyPressed) end[/CODE]
Thanks for clarifying... Instead of detecting a key-press to drain stamina ( which will drain even if the player isn't moving ), why not use the players velocity in a server-side movement hook? That way you can ensure the player:GetVelocity( ):Length( ) > player:GetWalkSpeed( ) before draining stamina... Besides, if you do it clientside, you'll make it easy to bypass which would allow cheaters to sprint forever... I was helping someone with a stamina system, so this isn't complete but it shows the major parts of a stamina system. The only thing left, if I recall correctly, was to add / subtract stamina ( which I just added ). The good thing about this system is that it is shared so the client and server would be in sync so no data would need to be networked aside from a few small instances such as when the stamina should reset on respawn / death, etc... Take a look to see how the stamina system could be accomplished in a different way which doesn't rely on hooks which don't always fire ( KeyPress/Release and a few others are problematic when multiple keys are pressed and/or held ): HTML: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_stamina_system/sh_basic_stamina_system.lua.html[/url] or Lua: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_stamina_system/sh_basic_stamina_system.lua[/url] Also, because the SetupMove hook already targets the player, _p, each player gets their own value without anyone sharing stamina values ( also because we used the player meta-table to add functions to )... Hopefully this helps.
Well i gonna try this way then with my own logic, i keep the subject open in case i got some problem and will post the result when finished and mark the topic as solved
It's always good to work through the problem on your own. Try not to over-complicate things ( it's easy to do but logic is simple so things that seem complicated may not be when converted over ).
Well i can't even test my code because my player class seems to not be loaded, 2 hours looking for my misstake and still not found yet. Maybe you can help me on this : [CODE]DEFINE_BASECLASS( "player_sandbox" ) local PLAYER = {} PLAYER.DisplayName = "CSRP Player Class" PLAYER.TeammateNoCollide = false PLAYER.WalkSpeed = 100 PLAYER.JumpPower = 300 PLAYER.RunSpeed = 160 PLAYER.__JumpOver = true PLAYER.__Stamina = 100 PLAYER.__Hunger = 100 PLAYER.__Thirst = 100 PLAYER.__Sleep = 100 function PLAYER:Loadout() self.Player:RemoveAllAmmo() self.Player:Give( "weapon_pistol" ) self.Player:GiveAmmo( 256, "Pistol", true ) self.Player:SwitchToDefaultWeapon() end player_manager.RegisterClass( "player_csrp", PLAYER, "player_sandbox") function GM:PlayerSpawn( ply ) ply:SetArmor( 100 ) MsgN( ply:Nick() .. " has spawned!" ) player_manager.SetPlayerClass( ply, "player_csrp" ) end [/CODE] i did the exact same thing that i saw on wiki and it's not working. I don't understand.
i close this thread and open a new one about my player class problem because i'm off-topic.
Sorry, you need to Log In to post a reply to this thread.