!HELP! Lua Help

I’m fairly new to Lua and I tried this if then block within a function touch block and it doesn’t seem to be working. What am I missing?

local tl = 99
local th = 100

if ( activator:IsPlayer() and activator:Health() <= tl ) then
	activator:ChatPrint('You have been healed.')
elseif ( activator:IsPlayer and activator:Health() >= th ) then
	activator:ChatPrint('You are already at full health.')
end

end

Where is the rest of your code. Did you try using prints to see what the cause of the if statement failing was?

[editline]27th April 2015[/editline]

You also have a syntax error line 7


function ENT:Touch( activator, caller )

	if ( activator:IsPlayer() ) then -- If a player, not an NPC or prop or anything, activates it.
		self:Remove()
		local health = activator:Health() -- Defines health as the activators health "activator:Health()".
		activator:SetHealth( math.Clamp( ( health or 100 ) + 25, 0, 100 ) ) -- !! LOOK UP "math.Clamp"
		activator:EmitSound( 'items/smallmedkit1.wav' )
	end

	local tl = 99
	local th = 100

	if ( activator:IsPlayer() and activator:Health() <= tl ) then
		activator:ChatPrint('You have been healed.')
	elseif ( activator:IsPlayer and activator:GetMaxHealth() and activator:Health() >= th ) then
		activator:ChatPrint('You are already at full health.')
	end
end 

No I don’t know this print method, and syntax error?

Please wrap your code in code tags [cod.e] [/cod.e] (without periods). Your syntax error is that you have two ends on your second if statement. If you have if/elseif/else statements, you only need one end per statement (the rod dot and highlighting denotes a syntax error):

As for the prints, you just use the

Global.print function throughout your code to see which part does not work. If your code works correctly, you should see a server console message when your conditions are met.

http://puu.sh/hsONO/123eff28d3.png

There are multiple lines, the first end is to end the if then statement then the second end is to end the function, do I still need just one end? And could you give me an example for the global.print? And my bad baby c; I’ll just puush them from now on.

Ah, I was responding to what James said about your first post (where there was a syntax error). No, in that case, you still need two ends. However, you do have a syntax error at line 74, where you forgot to put parentheses after the ent:IsPlayer() function. Anyway, just put prints wherever you can to see what does not get run.




function ENT:Touch( activator, caller )
	print('Entity was touched.')
	if ( activator:IsPlayer() ) then -- If a player, not an NPC or prop or anything, activates it.
		self:Remove()
		local health = activator:Health() -- Defines health as the activators health "activator:Health()".
		activator:SetHealth( math.Clamp( ( health or 100 ) + 25, 0, 100 ) ) -- !! LOOK UP "math.Clamp"
		activator:EmitSound( 'items/smallmedkit1.wav' )
		print('activator is player')
	end

	local tl = 99
	local th = 100

	if ( activator:IsPlayer() and activator:Health() <= tl ) then
		print('activator has less than full health and was healed')
		activator:ChatPrint('You have been healed.')
	elseif ( activator:IsPlayer() and activator:GetMaxHealth() and activator:Health() >= th ) then -- You forgot the parentheses.
		print('activator has full health and was not healed')
		activator:ChatPrint('You are already at full health.')
	end
end


Oh, wow, I didn’t even read through it. So after I was done checking this out I tried going about it a different way because with the current script it does the first few lines first which are awkward because it shouldn’t activate if the player has full health. I rewrote it.


 function ENT:Touch( activator, caller )

	if ( activator:IsPlayer() ) then -- If a player, not an NPC or prop or anything, activates it.
		local health = activator:Health() -- Defines health as the activators health "activator:Health()".
		local fuhp = ('You are already at full health.')
		local cp1 = activator:ChatPrint(fuhp)
		if (health <= 99) then
			activator:EmitSound( 'items/smallmedkit1.wav' )
			activator:SetHealth( math.Clamp( ( health or 100 ) + 25, 0, 100 ) )
			self:Remove()
			activator:ChatPrint('You have been healed.')
		elseif ( health >= 100 ) then
			activator:ChatPrint('You are already at full health.')
		end
	end
end 

Now I’m facing the problem of spam with activator:ChatPrint(‘You are already at full health.’). Every time I bump it, the line is spammed in chat. What class should I use to create some kind of anti-spam?

Change your entity’s use type to SIMPLE_USE.

e: lol didn’t read

That was the syntax error I was on about. I ignored the extra end because I assumed he just pasted the end to the function within which his code was.

I would Luni but with what I have planned I can’t have the use function, otherwise this would be so much easier.

This is the much less ideal way of doing it, but you can add a variable on the player of the next time they can use the entity. If the value is less than CurTime(), then return. This will not prevent it from firing multiple times, but it will make sure that the rest of the function will not run.



function ENT:Touch( activator, caller ) -- Defines health as the activators health "activator:Health()".
	--Why did you declare variables you don't use?
	if not activator:IsPlayer() or (activator.NextHealEntUse and activator.NextHealEntUse < CurTime()) then return end
	local health = activator:Health()
	activator.NextHealEntUse = CurTime() + 1.5 -- May need to increase it.
	if health <= 99 then
		activator:EmitSound( 'items/smallmedkit1.wav' )
		activator:SetHealth( math.Clamp( ( health or 100 ) + 25, 0, 100 ) )
		self:Remove()
		activator:ChatPrint('You have been healed.')
	elseif health >= 100 then
		activator:ChatPrint('You are already at full health.')
	end
end


Also, I don’t see when you need the elseif, why not just an else. Also, why check if the health is less than or equal to 99 when you can just do less than 100. It doesn’t change anything in the long run, but still.