SWEP help: Motion Blur on run

I am working on a SWEP that makes the character move quicker, and figured the motion blur would be a neat effect. But, I don’t know how to bind a SWEP function to a keyboard key (in this case, the Shift key). Can someone give me a line of LUA code, or some help with what I’m trying to do here? (I have searched the forums already, and I can’t find anything!)

[lua]if self.Owner:KeyDown(IN_SPEED) then
– motion blur
end[/lua]

Wow, that was fast. Thanks much!

No problem :smile:

Okay uh, now I have a new problem. So I just plugged in the code, and it looked like this:

if self.Owner:KeyDown(IN_SPEED) then
self.Owner:DrawMotionBlur( 0.02, 0.5, 0)
end

It wouldn’t let me use the weapon in Gmod, so I went back and changed it to this:

function SWEP:KeyDown(IN_SPEED)
self.Owner:DrawMotionBlur( 0.02, 0.5, 0)
end

The weapon is useable now, but there is no motion blur effect when I press shift. Any suggestions?

Don’t use the second code, it won’t work.

When you use the first one you should get an error in the console, can you post it?

probably that self is nil. Did you put it in a HudDraw hook!

Here’s the error I came up with:

weapons\weapon_*****_swep\shared.lua:67: attempt to index global ‘self’ (a nil value)

The five astricts are the name of the weapon, I just replaced it so no one knows what it is. The weapons full name is *****_swep

As commander204 said you need to put it in a weapon function, for instance SWEP.HUDPaint.

I am not sure what that is to be honest… Here, I’ll just post the whole script (It’s not super long). Maybe things will be easier then.

[lua]
if ( SERVER ) then
AddCSLuaFile( “shared.lua” )
AddCSLuaFile( “cl_init.lua” )
end

SWEP.PrintName = “*****”
SWEP.Author = “Kryanwan8”
SWEP.Contact = “”
SWEP.Purpose = “”
SWEP.Instructions = “”
SWEP.Category = “Kryanwan8”
SWEP.Slot = 2
SWEP.Slotpos = 0
SWEP.BounceWeaponIcon = true

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.ViewModelFlip = true

SWEP.ViewModel = “”
SWEP.WorldModel = “”

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

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

function SWEP:Deploy()
self.Weapon:EmitSound( “weapons/*****.wav” )
self.Owner:SetRunSpeed(4250)
self.Owner:SetWalkSpeed(1500)
self.Owner:SetJumpPower(300)
return true
end

function SWEP:PrimaryAttack()
self.Weapon:EmitSound( “weapons/*****.wav” )
self.Weapon:SetNextPrimaryFire( CurTime() + 2.5 )
self.Weapon:SetNextSecondaryFire( CurTime() + 2.5 )
end

function SWEP:SecondaryAttack()
self.Weapon:EmitSound( “weapons/*****.wav” )
self.Weapon:SetNextSecondaryFire( CurTime() + 1.2 )
self.Weapon:SetNextPrimaryFire( CurTime() + 1.2 )
end

if self.Owner:KeyDown(IN_SPEED) then
self.Owner:DrawMotionBlur( 0.02, 0.5, 0)
end
[/lua]

Could you post it in [noparse][lua][/lua][/noparse] tags, makes it easier for us to read.

if (CLIENT) then
LocalPlayer():DrawMotionBlurHereNShit
end

Hope that helps :stuck_out_tongue:

[lua]
function SWEP:HudPaint()
if self.Owner:KeyDown(IN_SPEED) then
self.Owner:DrawMotionBlur( 0.02, 0.5, 0)
end
[/lua]

Like this?

I don’t think DrawMotionBlur is a player method, read this: **[G.DrawMotionBlur

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=G.DrawMotionBlur)**.

No point in using KeyDown for motion blur.

[lua]function SWEP:RenderScreenspaceEffects() – Change to HUDPaint or Think if this does not exist.
if self.Owner:GetVelocity():Length() > 80 then
DrawMotionBlur( 0.02, 0.5, 0)
end
end
[/lua]

What if the gamemode changes the walk speed to greater than 80? Then you would get motion blur even if you aren’t sprinting.

Oh. Didn’t read the section of the OP that only requested it for sprinting. Just noticed now.

I think I can use both Nullpoint’s and Flapjack’s code combined. Just a little editing, and this might work out:

[lua]function SWEP:RenderScreenspaceEffects() – Change to HUDPaint or Think if this does not exist.
if self.Owner:KeyDown(IN_SPEED) then
DrawMotionBlur( 0.02, 0.5, 0)
end
end
[/lua]

Does this sound any better?

Yes, but I don’t think that SWEP function exists. You could make it though by putting this code anywhere clientside, bear in mind though that it may not work.

[lua]hook.Add(“RenderScreenspaceEffects”, “WeaponRenderSSEffects”, function()
local w = LocalPlayer():GetActiveWeapon();
if w and w.RenderScreenspaceEffects then
w:RenderScreenspaceEffects();
end
end);[/lua]

If I need to, I could just remove this code if it doesnt work, and swap RenderScreenspaceEffects wtih HudPaint or Think like he suggested, right?