1. Post #1
    banini's Avatar
    July 2012
    56 Posts
    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_mp5",
    "weapon_glock",
    "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.
                    else
                            ply:SetVar("togglevis", "off")
                            ply:EmitSound(Sound("items/nvg_off.wav"))
                            return true
                    end
            end
    end
    
    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.SetMaterial(Material("sprites/bluelaser1"))
    		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.SetMaterial(Material("Sprites/light_glow02_add_noz"))
    		render.DrawQuadEasy(tr.HitPos, (EyePos() - tr.HitPos):GetNormal(), Size, Size, Color(255,0,0,255), 0)
    		cam.End3D()
     
                    end
     
            end
    end
    
    Thanks guys!

    (User was banned for this post ("Undescriptive thread title" - Gran PC))
    Reply With Quote Edit / Delete Windows 7 China Show Events Dumb Dumb x 1 (list)

  2. Post #2
    TheTrueAndy's Avatar
    September 2009
    446 Posts
    First of all, use the BBCode
    Code:
    [lua] 
    Your code here
    [/ lua]        
    
     (without the space between "/" and "lua")
    So we don't get cancer in the eyes.
    Reply With Quote Edit / Delete Windows XP Germany Show Events

  3. Post #3
    Honk!
    TSIThomas's Avatar
    December 2008
    247 Posts
    you failed at bbcode, it's [lua][/lua]. :3:

    he fixed it!
    Reply With Quote Edit / Delete Windows 7 United Kingdom Show Events Winner Winner x 1 (list)

  4. Post #4
    banini's Avatar
    July 2012
    56 Posts
    guys?
    Reply With Quote Edit / Delete Windows 7 China Show Events

  5. Post #5
    TheTrueAndy's Avatar
    September 2009
    446 Posts
    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?
    	end
    end
    
    or debug.Trace()

    But here is what you've done wrong:
    Old Gmod Wiki posted:
    This function can only be used in draw functions such as the ENT:Draw hook for scripted entities (see above example), cam functions, or effects.
    (You're trying to call it in HUDPaint().)

    Edited:
    Sorry, that's wrong. You have a cam function there.
    Reply With Quote Edit / Delete Windows XP Germany Show Events Lua Helper Lua Helper x 1 (list)

  6. Post #6
    banini's Avatar
    July 2012
    56 Posts
    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?
    	end
    end
    
    or debug.Trace()

    But here is what you've done wrong:

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

    Edited:
    Sorry, that's wrong. You have a cam function there.
    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 :)
    Reply With Quote Edit / Delete Windows 7 China Show Events

  7. Post #7
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    2,073 Posts
    Line 31.
    Change ply:GetActiveWeapon():GetPrintName() to
    ply:GetActiveWeapon():GetClass()
    Reply With Quote Edit / Delete Windows 7 Canada Show Events Informative Informative x 1 (list)

  8. Post #8
    banini's Avatar
    July 2012
    56 Posts
    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)
     if vm and ply:GetActiveWeapon() != NULL and table.HasValue(AcceptedLaserWeps, ply:GetActiveWeapon():GetClass() then 
    Reply With Quote Edit / Delete Windows 7 China Show Events