I am sorry, but a noobish question...

Hi guys,
I am having an issue with a laser script.
It is not how to make one, but how to make it work.
I semi-coded one but it still doesn’t appear.
I have made it clientside as the Gmod wiki said, but I dont know if thats the problem.
Here’s the code:
(the file’s name is cl_laser.lua)
AcceptedLaserWeps = {
“weapon_m4a1” }

function GM:PlayerBindPress( ply, bind, pressed )
if not pressed then return false end //Return if they’re releasing the key or else it gets called twice.

local ply = LocalPlayer()
local togglevis = ply:GetVar(“togglevis”, “off”)
if bind == “impulse 100” then
if togglevis == “off” then
//Turn the laser on!
ply:SetVar(“togglevis”, “on”)
ply:EmitSound(Sound(“items/nvg_on.wav”),100,160) //A nice sound.
return true //This disables the flashlight.
ply:SetVar(“togglevis”, “off”)
return true

function GM:HUDPaint()
local ply = LocalPlayer()

    if (ply:GetVar("togglevis", "off") == "on") then //If his laser is turned on, then do the following:
    local vm = ply:GetViewModel()

	if vm and ply:GetActiveWeapon() != NULL and table.HasValue(AcceptedLaserWeps, ply:GetActiveWeapon():GetPrintName()) then
	local attachmentIndex = vm:LookupAttachment("1")

	if attachmentIndex == 0 then attachmentIndex = vm:LookupAttachment("muzzle") end //CS:S guns use different attachment names.

            local t = util.GetPlayerTrace(ply)
	local tr = util.TraceLine(t)
        cam.Start3D(EyePos(), EyeAngles())
	//Draw the laser beam.
	render.DrawBeam(viewModel:GetAttachment(attachmentIndex).Pos, tr.HitPos, 2, 0, 12.5, Color(255, 0, 0, 255))
	//Draw a laser dot at the end of the beam.
	local Size = math.random() * 1.35 //That .65 makes all the difference
	render.DrawQuadEasy(tr.HitPos, (EyePos() - tr.HitPos):GetNormal(), Size, Size, Color(255,0,0,255), 0)



Thanks guys!

(User was banned for this post ("Undescriptive thread title" - Gran PC))

First of all, use the BBCode

Your code here
[/ lua]        

 (without the space between "/" and "lua")

So we don’t get cancer in the eyes. :v:

you failed at bbcode, it’s [noparse][lua][/lua][/noparse]. :3:

he fixed it!


If I have problems to find my bug, I use print()s between the “if” sentences.
print("#1!") – Got the function even called?
if (blah) then
print("#2!") – Got stuck on blah?
if (plop) then
print("#3!") – Got stuck on plop?
or debug.Trace()

But here is what you’ve done wrong:

(You’re trying to call it in HUDPaint().)

Sorry, that’s wrong. You have a cam function there. :v:

Im sorry, I really suck at Lua.
What do you mean by a cam function and which function for the quote?
Sorry, I just need a bit more clarity :slight_smile:

Line 31.
Change ply:GetActiveWeapon():GetPrintName() to

Thanks a lot guys for the advice!
I got it to load but it gives me crap that:
(error) [gamemodes\zmod\gamemode\cl_laser.lua:31] ‘)’ expected near ‘then’
(line 31) [lua] if vm and ply:GetActiveWeapon() != NULL and table.HasValue(AcceptedLaserWeps, ply:GetActiveWeapon():GetClass() then [/lua]