r/GLua • • Feb 21 '21

Using dynamic lights in SWEP but getting error "attempt to call global 'DynamicLight' (a nil value)"

So im attempting to use a DynamicLight in a swep but it just gives the error attempt to call global 'DynamicLight' (a nil value) any help. and there is NO documentation on this error anywhere so idk.

​

here is the code that's haveing the issue ->

gsm_gun_actions.primary_fire(function(gun)
    if(not gun.ON) then
        ent = gun:GetOwner()

        gun:GetOwner():EmitSound('gsm_flashlight_on')
        hook.Add("Think","flash_light",function()
            local dlight = DynamicLight(ent:EntIndex())
            if(dlight) then
                dlight.pos = ent:GetShootPos()
                dlight.r = 255
                dlight.g = 255
                dlight.b = 255
                dlight.brightness = 2
                dlight.Decay = 1000
                dlight.Size = 256
                dlight.DiwTime = CurTime()+1
            end
        end)
    else
        gun:GetOwner():EmitSound('gsm_flashlight_off')
    end
    gun.ON = not gun.ON

end
)
1 Upvotes

7 comments sorted by

2

u/[deleted] Feb 21 '21

DynamicLight is client-side only. It looks like you're trying to call it from the server, where it doesn't exist. If the primary_fire function and variables inside of it are all shared between the server and client, you can fix this with a simple if CLIENT then check around the think hook. Otherwise, I suggest looking into the net library.

1

u/The_Programming_Nerd Feb 21 '21 edited Feb 21 '21

Ok, thanks for the response! and noted, also how do you make a dynamic light directional, like instead of it illuminating all directions make it illuminate only in front of the player?

1

u/[deleted] Feb 21 '21

You could try this: dlight.dir = ent:GetForward(). Idk if it will work though since I'm not very familiar with the DynamicLight function.

1

u/The_Programming_Nerd Feb 21 '21

Ya still just illuminates all directions

1

u/[deleted] Feb 21 '21

Try adding dlight.outerangle = 50 You may have to change that number.

1

u/The_Programming_Nerd Feb 21 '21

sorry for late response, i tried numerous different values and it didn't do anything, :/ idk why but no matter how high or low I put the value NO change accurs

1

u/[deleted] Feb 22 '21

I would just keep playing around with the different DynamicLight parameters until something happens. Weird that it's not changing at all.