Move speed set by Weapon Equiped?

Recently I have tried to make more complicated weapons as I have only done simple things before this. I have a machine gun that I want to slow down the player while it is equipped. So during the deploy and holster functions I have it set another function to true or false (which this function controls the playerspeed). I feel like this is overly complicated. I have tried to use :setwalkspeed(x) during the deploy and holster functions but I get errors complaining that “self” or “self.owner” or “ply” or “whatever i put in front of :setwalkspeed” is not a global variable…which I don’t see why that matters. As of now the code runs with no errors but does nothing for this section.

Anyways all I’m asking is how to fix this code. Or make code that does the same function in a more simplistic way.



function SWEP:Deploy()
	self.Owner.movedeployspeed = true
	return true
end
function SWEP:Holster()
	self.Owner.movedeployspeed = false
	return true
end
local function deployspeed()
	if ent.movedeployspeed and ent:IsPlayer() then
	self:SetWalkSpeed(45)
	end
end
hook.Add("Deploy", "deployspeed", deployspeed)


I don’t even fully understand this code I’ve written as alot of this is based on what I’ve either experimented with or what I just guessed by seeing it on the Wiki. I have searched all the setwalkspeed/runspeed posts and looked on Google and examined other lua code and I cannot find any solution. I think my problem is with the “hook.Add”.

Thank you.



function SWEP:Deploy()
	self.Owner:SetWalkSpeed(45)
	return true
end

function SWEP:Holster()
	self.Owner:SetWalkSpeed(250)
	return true
end


worked fine for me

Wow, thank you. I assume the reason it didn’t work before was because I was trying to use ply:SetWalkSpeed(x)? I’m glad it was just a simple fix like this instead of something more complicated like I was trying to do.