How do i import the surface and draw libraries in lua

Ok, so my friend is making a scanner welder swep, and gets this error :


weapons\scanner_welder\shared.lua:108: attempt to index global 'draw' (a nil value)

I dont know a thing about what that means, i donk know lua. Just answer the question in the thread title

-snip-

[editline]03:09PM[/editline]

Post the code

Too late, I clicked the mighty box. :v:
Nah, the problem is that your code runs on both client and server (because it’s in shared.lua), while draw and surface functions only work clientside. Just add “if CLIENT then” at the beginning and “end” at the ending of the part where you use those functions.

I figured he already did that

He wrote
if (!CLIENT) then
but it still gives him the error

Because you need to call if CLIENT then. You’re calling if not CLIENT then.

Ok, he wanted me to post this :

The text is only drawn for a single frame if you do that 0 once it finishes there and goes to the next frame, it never gets drawn again unless that function is still running.

Your friend can:

Send it to the player in a chat message ( self.Owner:ChatPrint( “You can’t spawn it there!” ) )
Send it to the player with a usermessage and use the surface functions clientside in a HUDPaint function

Those are pretty much the options.

Hi, I tried to do what you said, but when I use if CLIENT the code never gets executed (there is a sound before the part that gives me an error, and I don’t hear it).
Here is the whole code.

[lua]

// Variables that are used on both client and server

SWEP.Author = “”
SWEP.Contact = “”
SWEP.Purpose = “”
SWEP.Instructions = “Shoot a prop to attach a City Scanner.
Right click to attach a Claw Scanner.”

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.ViewModel = “models/weapons/v_physcannon.mdl”
SWEP.WorldModel = “models/weapons/w_physcannon.mdl”

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = “none”

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = “none”

local ShootSoundCTS = Sound( “Bounce.Shrapnel” )
local ShootSoundCLS = Sound( “Bounce.Shrapnel” )
local NotAllowed = Sound( “Buttons.snd10” )

/---------------------------------------------------------
Reload does nothing
---------------------------------------------------------
/
function SWEP:Reload()
end

/---------------------------------------------------------
Think does nothing
---------------------------------------------------------
/
function SWEP:Think()
end

/---------------------------------------------------------
PrimaryAttack
---------------------------------------------------------
/
function SWEP:PrimaryAttack()

local tr = self.Owner:GetEyeTrace()
if ( tr.HitWorld ) then 
	if CLIENT then
		self.Owner:ChatPrint( "You can't spawn it there!" )
		self:EmitSound( NotAllowed )
		//ply:PrintMessage(HUD_PRINTNOTIFY, "Weapon drop disabled")
		surface.SetFont( "TargetID" );
		
		surface.SetTextPos( 51, 51 );
		surface.SetTextColor( Color(150, 0, 0, 255) );
		surface.DrawText( "You can't spawn it there!" );
	 
		surface.SetTextPos( 50, 50 );
		surface.SetTextColor( Color(230, 0, 0, 255) );
		surface.DrawText( "You can't spawn it there!" );
	
	
	draw.SimpleText("You can't spawn a City Scanner there", "ScoreboardText", 100, 50, Color(86, 104, 86, 255), 0, 0)
	end
	return end

local effectdata = EffectData()
	effectdata:SetOrigin( tr.HitPos )
	effectdata:SetNormal( tr.HitNormal )
	effectdata:SetMagnitude( 8 )
	effectdata:SetScale( 1 )
	effectdata:SetRadius( 16 )
util.Effect( "Sparks", effectdata )

self:EmitSound( ShootSoundCTS )

self:ShootEffects( self )


// The rest is only done on the server
if (!SERVER) then return end

// Make a manhack
local ent = ents.Create( "npc_cscanner" )
	ent:SetPos( tr.HitPos + self.Owner:GetAimVector() * -16 )
	ent:SetAngles( tr.HitNormal:Angle() )
ent:Spawn()

// Weld it to the object that we hit
local weld = constraint.Weld( tr.Entity, ent, tr.PhysicsBone, 0, 0 )

undo.Create("City Scanner")
	undo.AddEntity( weld )
	undo.AddEntity( nocl )
	undo.AddEntity( ent )
	undo.SetPlayer( self.Owner )
undo.Finish()

end

/---------------------------------------------------------
SecondaryAttack
---------------------------------------------------------
/
function SWEP:SecondaryAttack()

local tr = self.Owner:GetEyeTrace()
if ( tr.HitWorld ) then 
	self:EmitSound( NotAllowed )
	draw.SimpleText("You can't spawn a Claw Scanner there", "ScoreboardText", 100, 50, Color(86, 104, 86, 255), 0, 0)
	return end

self:EmitSound( ShootSoundCLS )
self:ShootEffects( self )

local effectdata = EffectData()
	effectdata:SetOrigin( tr.HitPos )
	effectdata:SetNormal( tr.HitNormal )
	effectdata:SetMagnitude( 8 )
	effectdata:SetScale( 1 )
	effectdata:SetRadius( 16 )
util.Effect( "Sparks", effectdata )


// The rest is only done on the server
if (!SERVER) then return end

// Make a manhack
local ent = ents.Create( "npc_clawscanner" )
	ent:SetPos( tr.HitPos + self.Owner:GetAimVector() * -16 )
	ent:SetAngles( tr.HitNormal:Angle() )
ent:Spawn()

// Weld it to the object that we hit
local weld = constraint.Weld( tr.Entity, ent, tr.PhysicsBone, 0, 0 )

undo.Create("Claw Scanner")
	undo.AddEntity( weld )
	undo.AddEntity( nocl )
	undo.AddEntity( ent )
	undo.SetPlayer( self.Owner )
undo.Finish()

end

/---------------------------------------------------------
Name: ShouldDropOnDie
Desc: Should this weapon be dropped when its owner dies?
---------------------------------------------------------
/
function SWEP:ShouldDropOnDie()
return false
end
[/lua]

help

.

Way to increase post count. Try this TerabyteS (untested)

[lua]function SWEP:PrimaryAttack()

local tr = self.Owner:GetEyeTrace()
if ( tr.HitWorld ) then 
	if CLIENT then
		self.Owner:ChatPrint( "You can't spawn it there!" )
		self:EmitSound( NotAllowed )
		
		hook.Add( "HUDPaint", "SpawnNotAllowed", function( )
			surface.SetFont( "TargetID" );
		
			surface.SetTextPos( 51, 51 );
			surface.SetTextColor( Color(150, 0, 0, 255) );
			surface.DrawText( "You can't spawn it there!" );
	 
			surface.SetTextPos( 50, 50 );
			surface.SetTextColor( Color(230, 0, 0, 255) );
			surface.DrawText( "You can't spawn it there!" );
	
			draw.SimpleText("You can't spawn a City Scanner there", "ScoreboardText", 100, 50, Color(86, 104, 86, 255), 0, 0)
		end)
		
		timer.Simple( 3, function() hook.Remove( "HUDPaint", "SpawnNotAllowed" ) ) 
	end
	return
end

local effectdata = EffectData()
	effectdata:SetOrigin( tr.HitPos )
	effectdata:SetNormal( tr.HitNormal )
	effectdata:SetMagnitude( 8 )
	effectdata:SetScale( 1 )
	effectdata:SetRadius( 16 )
util.Effect( "Sparks", effectdata )

self:EmitSound( ShootSoundCTS )

self:ShootEffects( self )


// The rest is only done on the server
if (!SERVER) then return end

// Make a manhack
local ent = ents.Create( "npc_cscanner" )
	ent:SetPos( tr.HitPos + self.Owner:GetAimVector() * -16 )
	ent:SetAngles( tr.HitNormal:Angle() )
ent:Spawn()

// Weld it to the object that we hit
local weld = constraint.Weld( tr.Entity, ent, tr.PhysicsBone, 0, 0 )

undo.Create("City Scanner")
	undo.AddEntity( weld )
	undo.AddEntity( nocl )
	undo.AddEntity( ent )
	undo.SetPlayer( self.Owner )
undo.Finish()

end[/lua]

Just replace the primary fire function with this.

It’s called a blankquote.

Useless, if you don’t read posts.

But you didn’t pay attention to the post the first time, nor the second time.

I said the code doesn’t get executed because I don’t even hear the sound, so you don’t need to use blank quotes about stuff I already read and know.

MakeR, I tried to add your part in my file, but now, when I go to the menu in gmod, I don’t even see the button to spawn it (and if I remove that part I do). I don’t know why gmod does this, but can it be about some syintax error somewhere?

Check the console for errors.

–retarded question–

What’s a snip?

A post that’s been removed.