• Print

Author Topic: surrenderScript  (Read 4528 times)

0 Members and 1 Guest are viewing this topic.

Offline Suicidal Dog

  • Newbie
  • *
  • Posts: 14
  • Karma: -1
surrenderScript
« on: June 22, 2017, 04:23:02 am »
I am trying to make a script that will force a player to be stripped and slowed when their health is lower than 1, I want to also make it so when they are healed up they will then receive all their weapons back. I have some of it but it doesn't seem to identify the player is less than a certain health amount and stop them taking damage.

Code: Lua
  1.  
  2. local surrenderTeams = {
  3.         [TEAM_CADET]=true,
  4. }
  5.  
  6. hook.Add("PlayerShouldTakeDamage","SurrenderHook",function( ply, att )
  7.  
  8.         if surrenderTeams[ply:Team()] then
  9.  
  10.                 if ply:Health() <= 1 then
  11.  
  12.                         ply:SetHealth(1)
  13.                         ply:SetWalkSpeed(100)
  14.                         ply:SetRunSpeed(100)
  15.                         ply:SetCrouchedWalkSpeed(100)
  16.                         return false
  17.  
  18.                 end
  19.  
  20.         end
  21.  
  22. end)
« Last Edit: June 22, 2017, 04:25:44 am by Suicidal Dog »

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: surrenderScript
« Reply #1 on: June 22, 2017, 10:55:31 am »
That script does work to some extent:

If I attack someone for 5 damage and my opponent has 1 HP remaining. -> Opponent does not take damage.
If I attack someone for 5 damage and my opponent has 2 HP remaining. -> Opponent dies because of damage.

Problem is that ply:Health() returns the player’s health before damage is taken.

Suggestion: check if the damage is lethal instead.

Tip #1: You can get damage info and suppress damage with the GM:EntityTakeDamage hook.

Tip #2: Example of how to use the hook to detect and suppress lethal damage:
Code: Lua
  1. hook.Add( "EntityTakeDamage", "SurrenderHook", function ( target, dmg )
  2.     if target:Health() <= dmg:GetDamage() then
  3.         -- TODO: Strip player
  4.         -- TODO: Apply slowness
  5.         ply:SetHealth( 1 )
  6.         return true -- Suppress lethal damage
  7.     end
  8. end )
« Last Edit: June 24, 2017, 12:54:25 am by Timmy »

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: surrenderScript
« Reply #2 on: June 28, 2017, 10:40:12 am »
Very well put Timmy!
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

  • Print