• Print

Author Topic: Why does this not work correctly?  (Read 6671 times)

0 Members and 1 Guest are viewing this topic.

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Why does this not work correctly?
« on: July 31, 2015, 05:28:32 am »
Code: Lua
  1. ITEM.Name = "Regeneration"
  2. ITEM.Price = 14000
  3. ITEM.Model = "models/healthvial.mdl"
  4. ITEM.Attachment = 'eyes'
  5.  
  6. function ITEM:OnEquip(ply, modifications)
  7.         ply:PS_AddClientsideModel(self.ID)
  8. end
  9.  
  10. function ITEM:OnHolster(ply)
  11.         ply:PS_RemoveClientsideModel(self.ID)
  12. end
  13.  
  14. function ITEM:ModifyClientsideModel(ply, model, pos, ang)
  15.         model:SetModelScale(1.0, 0)
  16.         pos = pos + (ang:Forward() * -6) + (ang:Up() * 10.5)
  17.  
  18.         return model, pos, ang
  19. end
  20.  
  21. function ITEM:Think(ply, modifications)
  22.         ply:SetMaxHealth( 100 )
  23.         ply:SetHealth( ply:Health() * 0.1 )
  24. end

Apparently it won't actually "regenerate", instead it sets your hp to 0, why?

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: Why does this not work correctly?
« Reply #1 on: July 31, 2015, 12:02:12 pm »
Think this one over a little bit....

Code: [Select]
ply:SetHealth( ply:Health() * 0.1 )

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Why does this not work correctly?
« Reply #2 on: July 31, 2015, 03:39:13 pm »
Think this one over a little bit....

Code: [Select]
ply:SetHealth( ply:Health() * 0.1 )

Shouldn't it multiply by 0.1? Sorry I'm not really good at maths :P

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Why does this not work correctly?
« Reply #3 on: July 31, 2015, 05:47:12 pm »
think about it practically.

If my health is 30/100
30 * 0.1 = 3
I just set my health to 3.

Next tick it does
3 * 0.1 = 0.3
I just set my health to 0.3


What you need to do is ADD.

ply:SetHealth( ply:Health() + ply:Health() * 0.1 )

This will do:

30 + 30*0.1 or 30 + 3
Setting my health to 33 the first tick.

You might want to clamp it at the max health though.




Try This:
ply:SetHealth( math.Clamp( ply:Health() + ply:Health() * 0.1, 0, ply:GetMaxHealth() ) )

Also, keep in mind that there are usually anywhere from 33 to 100 ticks per second. If you increase the health by 0.1 per tick they are going to go to 100% VERY fast. You might want to make that number smaller.

Also, instead of increasing the health by a exponent of their current health, why not just have it go up a set amount every tick?
ply:SetHealth( math.Clamp( ply:Health() + 0.1, 0, ply:GetMaxHealth() ) )

Think about how long this effect lasts.

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Why does this not work correctly?
« Reply #4 on: August 01, 2015, 01:13:59 am »
Okay, thanks for the explanation, I had no idea how maths work in lua coding language, I'm only used to 10 + 10 + 78 not lua + lua * more lua. xD

[EDIT] Why am I getting this error, it is defined by gmod itself, do I need to define it locally or is it suppose to not work?
Code: Text
  1. [ERROR] addons/pointshop-master/lua/pointshop/items/powerups/regen.lua:22: attempt to call method 'SetMaxHealth' (a nil value)
  2.   1. unknown - addons/pointshop-master/lua/pointshop/items/powerups/regen.lua:22
  3.    2. fn - addons/pointshop-master/lua/pointshop/sh_init.lua:146
  4.     3. unknown - addons/ulib/lua/ulib/shared/hook.lua:179
« Last Edit: August 01, 2015, 01:33:44 am by Janjakob2000 »

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Why does this not work correctly?
« Reply #5 on: August 01, 2015, 01:48:30 am »
Is it clientside or server side error?

Yellow or Blue?

SetMaxHealth can only be called serverside.

Wrap the code inside your Think function with this...


if SERVER then
    do code here...
end


also.. math in lua is just like regular math. It's more like Algebra in that you can use variables instead of constant numbers.

local a = 10
local b = 5

print( a + b )

output would be 15

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Why does this not work correctly?
« Reply #6 on: August 01, 2015, 03:09:38 am »
It is Yellow, I'll edit my post if it doesn't work, thanks!

[EDIT] It doesn't work, is it supposed to look like this:
Code: Lua
  1. ITEM.Name = "Regeneration"
  2. ITEM.Price = 14000
  3. ITEM.Model = "models/healthvial.mdl"
  4. ITEM.Attachment = 'eyes'
  5.  
  6. function ITEM:OnEquip(ply, modifications)
  7.         ply:PS_AddClientsideModel(self.ID)
  8. end
  9.  
  10. function ITEM:OnHolster(ply)
  11.         ply:PS_RemoveClientsideModel(self.ID)
  12. end
  13.  
  14. function ITEM:ModifyClientsideModel(ply, model, pos, ang)
  15.         model:SetModelScale(1.0, 0)
  16.         pos = pos + (ang:Forward() * -6) + (ang:Up() * 10.5)
  17.  
  18.         return model, pos, ang
  19. end
  20.  
  21. if (SERVER) then
  22.         function ITEM:Think(ply, modifications)
  23.                 ply:SetMaxHealth( 100 )
  24.                 ply:SetHealth( math.Clamp( ply:Health() + 0.1, 0, 100 ) )
  25.         end
  26. end
« Last Edit: August 01, 2015, 03:29:46 am by Janjakob2000 »

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: Why does this not work correctly?
« Reply #7 on: August 01, 2015, 03:20:58 pm »
Where is your file located?

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Why does this not work correctly?
« Reply #8 on: August 01, 2015, 03:36:51 pm »
Code: [Select]
function ITEM:Think(ply, modifications)
        if SERVER then
        ply:SetMaxHealth( 100 )
        ply:SetHealth( math.Clamp( ply:Health() + 0.1, 0, 100 ) )
        end
end

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Why does this not work correctly?
« Reply #9 on: August 02, 2015, 02:35:00 am »
Code: [Select]
function ITEM:Think(ply, modifications)
        if SERVER then
        ply:SetMaxHealth( 100 )
        ply:SetHealth( math.Clamp( ply:Health() + 0.1, 0, 100 ) )
        end
end
Well I just learned something, I had no idea that was even possible xD +1 compliment to you sir <3

  • Print