SWEP Not working

Ok, I need some help here. So this is the code I am using, for my SWEP and it doesn’t seem to be working at all!

It’s located in /addons/drivers_license/lua/weapons/shared.lua

I am newer to Lua, so help is appreciated.


if ( SERVER ) then
    AddCSLuaFile ("shared.lua")
    SWEP.AutoSwitchTo        = true
    SWEP.AutoSwitchFrom        = false
else
  print ("Running Drivers License SWEP!")
   SWEP.DrawCrosshair        = false
    SWEP.PrintName            = "Drivers License"
end


if ( CLIENT ) then

print ("Running!")

SWEP.Author = "TomTheDuck"
SWEP.Contact = "tomtheduck123@icloud.com"
SWEP.Purpose = "Show your drivers license to the police!"
SWEP.Instructions = "Left Click, to display in chat!"

SWEP.Category = "TomTheDuck Sweps"

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.Slot				= 2
SWEP.SlotPos			= 4

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"

SWEP.Base = "weapon_cs_base"
SWEP.ViewModel = "models/weapons/v_rif_ak47.mdl" 
SWEP.WorldModel = "models/weapons/w_rif_ak47.mdl"


function SWEP:PrimaryAttack()


local ply = LocalPlayer()
chat.AddText( Color( 100, 100, 255 ), ply, ", you are holding ", Color( 100, 255, 100 ), ply:GetActiveWeapon():GetClass() )

end

Thanks

-Tom

You have to name the weapon something other than shared.lua. Also, remove the AddCSLuaFile call.

You could just put the shared.lua in a folder named whatever he wants the entity name to be.

Thats useless, though.

It is, but I like to do so. I’m very weird.

It just looks messier and requires an AddCSLuaFile call instead of the engine doing it for you

True

I guess if you don’t need cl_init and init it’s the best way to do things.

Hey

Thanks, so far it seems good, however now I receive these errors in game.

[ERROR] addons/drivers_license/lua/weapons/shared.lua:54: ‘<eof>’ expected near ‘end’

  1. unknown - addons/drivers_license/lua/weapons/shared.lua:0

[ERROR] addons/drivers_license/lua/weapons/weapon_drivers_license.lua:49: ‘end’ expected (to close ‘if’ at line 11) near ‘<eof>’

  1. unknown - addons/drivers_license/lua/weapons/weapon_drivers_license.lua:0

The code is still the same as it was at the top, but has been renamed and the AddCSLuaFile call has been removed.

[ERROR] addons/drivers_license/lua/weapons/shared.lua:54: ‘<eof>’ expected near ‘end’

Delete your old shared.lua file.

As for the other error, add an end between SWEP.SlotPos and SWEP.Primary

Ok, thank you very much I will try that now

[editline]4th May 2016[/editline]

Ok, did what you said and still got this. As for the function i had a derp moment. Fixed that, but here is the error

[ERROR] addons/drivers_license/lua/weapons/weapon_drivers_license.lua:49: ‘end’ expected (to close ‘if’ at line 11) near ‘<eof>’

  1. unknown - addons/drivers_license/lua/weapons/weapon_drivers_license.lua:0

Sound to me like you have an extra ‘end’ in there.

Repost your whole code


if ( SERVER ) then
    SWEP.AutoSwitchTo        = true
    SWEP.AutoSwitchFrom        = false
else
  print ("Running Drivers License SWEP!")
   SWEP.DrawCrosshair        = false
    SWEP.PrintName            = "Drivers License"
end


if ( CLIENT ) then

print ("Running!")

SWEP.Author = "TomTheDuck"
SWEP.Contact = "tomtheduck123@icloud.com"
SWEP.Purpose = "Show your drivers license to the police!"
SWEP.Instructions = "Left Click, to display in chat!"

SWEP.Category = "TomTheDuck Sweps"

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.Slot = 2
SWEP.SlotPos = 4

end

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"

SWEP.Base = "weapon_cs_base"
SWEP.ViewModel = "models/weapons/v_rif_ak47.mdl" 
SWEP.WorldModel = "models/weapons/w_rif_ak47.mdl"


function SWEP:PrimaryAttack()

return
chat.AddText( Color( 100, 100, 255 ), ply, ", you are holding ", Color( 100, 255, 100 ), ply:GetActiveWeapon():GetClass() )



end


Also anyone know how to print chat text, this does’t seem to be working

-snip- Disregard.

Edit: Why no just add the if CLIENT statement to the else section of if SERVER?


if ( SERVER ) then
    SWEP.AutoSwitchTo        = true
    SWEP.AutoSwitchFrom        = false
else
   print ("Running Drivers License SWEP!")
   SWEP.DrawCrosshair        = false
   SWEP.PrintName            = "Drivers License"

   SWEP.Author = "TomTheDuck"
   SWEP.Contact = "tomtheduck123@icloud.com"
   SWEP.Purpose = "Show your drivers license to the police!"
   SWEP.Instructions = "Left Click, to display in chat!"

   SWEP.Category = "TomTheDuck Sweps"

   SWEP.Spawnable = true
   SWEP.AdminSpawnable = true
 
   SWEP.Slot = 2
   SWEP.SlotPos = 4

end

Don’t return inside SWEP:PrimaryAttack. Also, chat.AddText doesn’t work serverside, meaning running it in singleplayer won’t do anything except error. Use self:GetOwner():ChatPrint( “message” ) if you want it shared. Also, if you’re still getting the error, save the file and reload the game/server.

Ok, all of that has been updated, but now I still get

[ERROR] addons/drivers_license/lua/weapons/weapon_drivers_license.lua:49: ‘end’ expected (to close ‘if’ at line 11) near ‘<eof>’

  1. unknown - addons/drivers_license/lua/weapons/weapon_drivers_license.lua:0

This is the errror when i load up the game.

[editline]4th May 2016[/editline]

Huh, will try that, does seem logical

[editline]4th May 2016[/editline]

Even with else I still get the error

[ERROR] addons/drivers_license/lua/weapons/weapon_drivers_license.lua:49: ‘end’ expected (to close ‘if’ at line 11) near ‘<eof>’

  1. unknown - addons/drivers_license/lua/weapons/weapon_drivers_license.lua:0
    **

Your lines on your errors haven’t changed, meaning you aren’t editing the correct file or you are not saving/reloading correctly.

Ok, I am editing the only lua file in the addon, the SWEP one, and then saving it and reloading the game

[editline]4th May 2016[/editline]

Ok, I still receive the error while loading the game, and The whole thing works fine, It is just a bit inconvenient.

[ERROR] addons/drivers_license/lua/weapons/weapon_drivers_license.lua:49: ‘end’ expected (to close ‘if’ at line 11) near ‘<eof>’

  1. unknown - addons/drivers_license/lua/weapons/weapon_drivers_license.lua:0

Yeah, I snipped my original comment because I didn’t see the PrimaryAttack function, so I thought the last end was part of if CLIENT for some reason.

And, what code_gs said, don’t return inside of PrimaryAttack.
Since you’re still getting the error, save your file and restart gmod to make sure you’re using the most recent copy of your SWEP.

Side not, if you need the chattext debug line, this should work.



if SERVER then
   util.AddNetworkString("DriversLicense.AddChat")
else
   net.Receive( "DriversLicense.AddChat", function( len, ply )
      chat.AddText( Color( 100, 100, 255 ), ply, ", you are holding ", Color( 100, 255, 100 ), ply:GetActiveWeapon():GetClass() )
   end)
end

function SWEP:PrimaryAttack()
   net.Start( "DriversLicense.AddChat" )
   net.Send( self.Owner )
end


Thank you all for your help, The problem is now fully resolved