Hopefully a quick Lua question.

I’m practicing some Lua and I’m wondering what the easiest way to go about doing this is:
A SWEP changes a player boolean to true. Within the cl_hud of the Gamemode, if this is true, then it draws text.

Within the SWEP:


function SWEP:SecondaryAttack()
	if SERVER then 
		self.Owner.toggle = true
		self:Remove()
	end
end

Within the Gamemode cl_hud.lua


function GM:DrawGameHUD()
	if LocalPlayer().toggle then
		drawTextShadow("Hello Facepunch", "HudHintTextLarge", 5, ScrH() - 135, Color(255,0,0,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
	end
end

I already realize I’m doing it wrong, but hopefully somebody can help me understand how to make this work. In the context of this code, Right Clicking with the SWEP should show “Hello Facepunch” on the screen even when the SWEP isn’t there. The reason I don’t want to GM:DrawGameHUD() in the SWEP itself is because the SWEP must be removed after it’s been used, so it would no longer draw the text if it’s been removed.

The issue with that is you’re using the “toggle” on the SERVER. The CLIENT will know nothing about it.

Since it’s the SWEP, use:

[lua]function SWEP:SecondaryAttack( )
if ( SERVER ) then
self.Remove( ); // This REMOVES the weapon
else
// CLIENT - IsFirstTimePredicted( ) ensures it only runs ONCE, because CLIENT-side code for SWEPs SPAM until the SERVER and CLIENT sync. That’s why this is needed, although, it wouldn’t matter how many times this was set to true, but if it was a proper toggle, then it would.
if ( IsFirstTimePredicted( ) ) then
self.Owner.toggle = true; // For a proper toggle use: self.Owner.toggle = !self.Owner.toggle;
end
end
end[/lua]

[lua]hook.Add( “HUDPaint”, “DrawOnHud”, function( )
local _p = LocalPlayer( );
if ( _p.toggle ) then
//…

end

end );[/lua]

Thanks Acecool. I appreciate the help.

Sure thing, please make sure you mark the topic solved in the upper-left-hand-corner to avoid confusion :slight_smile: