Stupid Swep question

[lua] function SWEP:SecondaryAttack()
if self.Owner:Health() >= self.Owner:GetMaxHealth() then
self.Weapon:EmitSound( FailSound, 60, 100 )
–self.Owner:SetHealth(100)
else
self.Owner:SetHealth( self.Owner:Health() + 18 )
self.Weapon:EmitSound( ShootSound, 60, 100 )

end
self.Weapon:SetNextSecondaryFire(CurTime() + 1)

end [/lua]

gives this error: [lua] [weapons\weapon_swep_healer\shared.lua:2] attempt to call method ‘GetMaxHealth’ (a nil value)
[/lua]

It isn’t an invalid function. And the max health of a player is set when he spawns.

I don’t understand what the problem is. I’m sure it’s really simple. It worked in single player.

Are you sure that all the letters are supposed to be capital?
If yes, try this:
[lua]
function SWEP:SecondaryAttack()
if self.Owner:Health() >= self.Owner:MaxHealth() then
self.Weapon:EmitSound( FailSound, 60, 100 )
–self.Owner:SetHealth(100)
else
self.Owner:SetHealth( self.Owner:Health() + 18 )
self.Weapon:EmitSound( ShootSound, 60, 100 )

end
self.Weapon:SetNextSecondaryFire(CurTime() + 1)

end
[/lua]
If it doesn’t work, do not blame me. I’m fairly new to lua.

MaxHealth() isn’t colored in my Gmod Lua module thing for my notepad++, but GetMaxHealth() is. I’ll try it though.

MaxHealth() Is not a function, but GetMaxHealth is. http://wiki.garrysmod.com/?title=Entity.GetMaxHealth You can tell whether it is a function or not by if it doesn’t change colors. (If you have the gmod Notepad ++)

I am not really sure what self.Owner is because I haven’t done a lot of SWEP making or anything… But you could try LocalPlayer instead of self.Owner

[lua] function SWEP:SecondaryAttack()

if SERVER then
if self.Owner:Health() >= self.Owner:GetMaxHealth() then
self.Weapon:EmitSound( FailSound, 60, 100 )
–self.Owner:SetHealth(100)
else
self.Owner:SetHealth( self.Owner:Health() + 18 )
self.Weapon:EmitSound( ShootSound, 60, 100 )

end
self.Weapon:SetNextSecondaryFire(CurTime() + 1)

end
end [/lua]

Entity:GetMaxHealth() is not shared, it only works on the server. By calling it in a shared file, it runs on both client and server, giving you a yellow error and telling you that the method is nil.

You could also try this:
[lua]
function SWEP:SecondaryAttack()
if self.Owner:Health() > 1000 then
self.Weapon:EmitSound( FailSound, 60, 100 )
–self.Owner:SetHealth(100)
else
self.Owner:SetHealth( self.Owner:Health() + 18 )
self.Weapon:EmitSound( ShootSound, 60, 100 )

end
self.Weapon:SetNextSecondaryFire(CurTime() + 1)

end
[/lua]
Or, you could try the method above and see if it works better for you.

Depends on the players set max health.

That would work probably, but I don’t want to do that. I’m making a gamemode and different types of players get different MaxHealth()s. Trying now.

Then you need to send the client their max health using umsg or datastream.

Datastream isn’t remotely suitable in this situation - max health is one number you can send as a long.

I wrote this a long time ago, so please forgive the sloppiness. Adds GetMaxHealth() to the client. Include on server and client (shared).

[lua]
local meta = FindMetaTable(“Player”)

if SERVER then

local oldsmh = meta.SetMaxHealth

function meta:SetMaxHealth(h)	
	if self:IsValid() then		
		umsg.Start("_GMHP")
			umsg.Entity(self)
			umsg.Short(h)
		umsg.End()
		
		oldsmh(self, h)			
	end	
end

else

local function smhp(ent, hp)
	if ent:IsValid() then	
		ent._RealMaxHealth = hp
	else
		timer.Simple(0.1, smhp, ent, hp)
	end
end

usermessage.Hook("_GMHP", function(um) 
	local ent = um:ReadEntity()
	local hp = um:ReadShort()
	smhp(ent, hp)
end)

function meta:GetMaxHealth()
	return self._RealMaxHealth or self:Health()
end

end
[/lua]

In case you are wondering, I added the timer to fix an error I was getting when the particular entity was NULL on the client.

Feel free to improve this, it’s just a rough draft to give you an idea how I did it.

Using if SERVER then
or something worked perfect. No errors. But, the sound doesn’t get played.

Edit: Read comments noticed this was posted.