How to change the player's speed on deploy?

I’m making a SWEP that when deployed it makes you go faster, then when you switch to A different weapon it sets are walk speed to normal. How would I do that?

self.Owner:SetWalkSpeed/SetMaxSpeed

I didn’t see any difference in walk speed. That and I can’t switch out of my SWEP.


SWEP.Author			= "MLGSniperKing" -- These two options will be shown when you have the weapon highlighted in the weapon selection menu
SWEP.Instructions		= "Increased movement speed and attack."

SWEP.Spawnable = true
SWEP.AdminOnly = false

SWEP.Category				= "One Piece"
SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo		= "none"



SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo		= "none"

SWEP.Weight			= 5
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false

SWEP.Slot			= 1
SWEP.SlotPos			= 2
SWEP.DrawAmmo			= false
SWEP.DrawCrosshair		= true




SWEP.ViewModel			= "models/weapons/v_smg1.mdl"
SWEP.WorldModel			= "models/weapons/w_smg1.mdl"

local ShootSound2 = "weapons/one piece/fist/Jet pistol.mp3"
local DeploySound2 = "weapons/one piece/fist/Second gear.mp3"


function SWEP:Deploy()
		self:EmitSound( DeploySound2 )
		self.Owner:SetWalkSpeed(2)
end

function SWEP:Holster()
	self.Owner:SetWalkSpeed(1)
end

function SWEP:PrimaryAttack()

	self.Weapon:SetNextPrimaryFire( CurTime() + 0.1 )


	self:ThrowChair( "models/props/luffy/fist/fist.mdl" )
	
	

end

--
-- Called when the rightmouse button is pressed
--
function SWEP:SecondaryAttack()

	-- Note we don't call SetNextSecondaryFire here because it's not
	-- automatic and so we let them fire as fast as they can click.

	-- Call 'ThrowChair' on self with this model
	self:ThrowChair( "models/props/luffy/fist/fist.mdl" )


end

--
-- A custom function we added. When you call this the player will fire a chair!
--
function SWEP:ThrowChair( model_file )

	--
	-- Play the shoot sound we precached earlier!
	--
	self:EmitSound( ShootSound2 )


	--
	-- If we're the client then this is as much as we want to do.
	-- We play the sound above on the client due to prediction.
	-- ( if we didn't they would feel a ping delay during multiplayer )
	--
	if ( CLIENT ) then return end

	--
	-- Create a prop_physics entity
	--
	local ent = ents.Create( "prop_physics" )

	--
	-- Always make sure that created entities are actually created!
	--
	if ( !IsValid( ent ) ) then return end

	--
	-- Set the entity's model to the passed in model
	--
	ent:SetModel( model_file )

	--
	-- Set the position to the player's eye position plus 16 units forward.
	-- Set the angles to the player'e eye angles. Then spawn it.
	--
	ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) )
	ent:SetAngles( self.Owner:EyeAngles() )
	ent:Spawn()


	--
	-- Now get the physics object. Whenever we get a physics object
	-- we need to test to make sure its valid before using it.
	-- If it isn't then we'll remove the entity.
	--
	local phys = ent:GetPhysicsObject()
	if ( !IsValid( phys ) ) then ent:Remove() return end


	--
	-- Now we apply the force - so the chair actually throws instead
	-- of just falling to the ground. You can play with this value here
	-- to adjust how fast we throw it.
	--
	local velocity = self.Owner:GetAimVector()
	velocity = velocity * 100000000
	velocity = velocity + ( VectorRand() * 1000 ) -- a random element
	phys:ApplyForceCenter( velocity )
	
	
	timer.Simple(1.5, function()
	if ent:IsValid() then ent:Remove() end
	end)
	--
	-- Assuming we're playing in Sandbox mode we want to add this
	-- entity to the cleanup and undo lists. This is done like so.
	--
	cleanup.Add( self.Owner, "props", ent )

	undo.Create( "Thrown_jet_punch" )
		undo.AddEntity( ent )
		undo.SetPlayer( self.Owner )
	undo.Finish()
end

bump

Uh, why are you making the speeds so low? They aren’t multipliers – start up your game and do lua_run print( Entity(1):GetWalkSpeed() ) to get an idea of what the default is. Also, you will probably have to do SetMaxSpeed as well.

Ok, The regular speed is 250, So I bumped up the speed and put in SetMaxSpeed but there is still no difference and I can switch to a different weapon.



SWEP.Author			= "MLGSniperKing" -- These two options will be shown when you have the weapon highlighted in the weapon selection menu
SWEP.Instructions		= "Increased movement speed and attack."

SWEP.Spawnable = true
SWEP.AdminOnly = false

SWEP.Category				= "One Piece"
SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo		= "none"



SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo		= "none"

SWEP.Weight			= 5
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false

SWEP.Slot			= 1
SWEP.SlotPos			= 2
SWEP.DrawAmmo			= false
SWEP.DrawCrosshair		= true




SWEP.ViewModel			= "models/weapons/v_smg1.mdl"
SWEP.WorldModel			= "models/weapons/w_smg1.mdl"

local ShootSound2 = "weapons/one piece/fist/Jet pistol.mp3"
local DeploySound2 = "weapons/one piece/fist/Second gear.mp3"


function SWEP:Deploy()
		self:EmitSound( DeploySound2 )
		self.Owner:SetWalkSpeed(999)
		self.Owner:SetMaxSpeed(999)
end

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

function SWEP:PrimaryAttack()

	self.Weapon:SetNextPrimaryFire( CurTime() + 0.1 )


	self:ThrowChair( "models/props/luffy/fist/fist.mdl" )
	
	

end

--
-- Called when the rightmouse button is pressed
--
function SWEP:SecondaryAttack()

	-- Note we don't call SetNextSecondaryFire here because it's not
	-- automatic and so we let them fire as fast as they can click.

	-- Call 'ThrowChair' on self with this model
	self:ThrowChair( "models/props/luffy/fist/fist.mdl" )


end

--
-- A custom function we added. When you call this the player will fire a chair!
--
function SWEP:ThrowChair( model_file )

	--
	-- Play the shoot sound we precached earlier!
	--
	self:EmitSound( ShootSound2 )


	--
	-- If we're the client then this is as much as we want to do.
	-- We play the sound above on the client due to prediction.
	-- ( if we didn't they would feel a ping delay during multiplayer )
	--
	if ( CLIENT ) then return end

	--
	-- Create a prop_physics entity
	--
	local ent = ents.Create( "prop_physics" )

	--
	-- Always make sure that created entities are actually created!
	--
	if ( !IsValid( ent ) ) then return end

	--
	-- Set the entity's model to the passed in model
	--
	ent:SetModel( model_file )

	--
	-- Set the position to the player's eye position plus 16 units forward.
	-- Set the angles to the player'e eye angles. Then spawn it.
	--
	ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) )
	ent:SetAngles( self.Owner:EyeAngles() )
	ent:Spawn()


	--
	-- Now get the physics object. Whenever we get a physics object
	-- we need to test to make sure its valid before using it.
	-- If it isn't then we'll remove the entity.
	--
	local phys = ent:GetPhysicsObject()
	if ( !IsValid( phys ) ) then ent:Remove() return end


	--
	-- Now we apply the force - so the chair actually throws instead
	-- of just falling to the ground. You can play with this value here
	-- to adjust how fast we throw it.
	--
	local velocity = self.Owner:GetAimVector()
	velocity = velocity * 100000000
	velocity = velocity + ( VectorRand() * 1000 ) -- a random element
	phys:ApplyForceCenter( velocity )
	
	
	timer.Simple(1.5, function()
	if ent:IsValid() then ent:Remove() end
	end)
	--
	-- Assuming we're playing in Sandbox mode we want to add this
	-- entity to the cleanup and undo lists. This is done like so.
	--
	cleanup.Add( self.Owner, "props", ent )

	undo.Create( "Thrown_jet_punch" )
		undo.AddEntity( ent )
		undo.SetPlayer( self.Owner )
	undo.Finish()
end

] lua_run Entity(1):SetWalkSpeed( 999 )
> Entity(1):SetWalkSpeed( 999 )…

Works fine. Looks like something else is overriding your speed.

Is there any command or something to help me figure out whats overriding my speed?

What gamemode are you running? What addons? You could also put those commands in a timer.

In in sandbox, I have a lot of mods installed but I do have two speed alternating mods, Speed balls, and Speed gun.

The just put your command in a timer.

Sorry for asking again but how do I do that?

You have a timer in your ThrowChair function…just do the exact same thing you did there

Would this work? timer.Simple(0.5 self.Owner:SetRunSpeed(999)self.Owner:SetWalkSpeed(999)end)

You need a comma after 0.5

I get an error in the console saying I need a ‘)’ near ‘end’ but it doesn’t make sense if I do.

timer.Simple(0.5, (self.Owner:SetRunSpeed(999))(self.Owner:SetWalkSpeed(999))end)

You need to have the function keyword with parentheses to signify the start of a new function and the definition of arguments (even though there are none for the timer.Simple callback).

Ok, so something more along the lines of this?

timer.Simple(0.5, function() self.Owner:SetRunSpeed(999) self.Owner:SetWalkSpeed(999) end)

Yes, that looks like it should work. For future reference, it’s probably a good idea to test your code and see if it works instead of asking us if it will work.

I’m using Visual Studio Code and it doesn’t tell me if i’m doing something wrong, even if I turn on error check. Maybe if I use notepad++.

[editline]8th May 2016[/editline]

Ok now it tells me that I need a ‘)’ near ‘end’.

timer.Simple(0.5, function() self.Owner:SetRunSpeed(999) self.Owner:SetWalkSpeed(999) end)

Would I put it in between (999) and ‘end’? Then I would have to put parentheses around self.Owner like this right?

timer.Simple(0.5, function() (self.Owner:SetRunSpeed(999)) (self.Owner:SetWalkSpeed(999)) end)

[editline]8th May 2016[/editline]

Wait sorry, let me text it first

[editline]8th May 2016[/editline]

Ok, now were getting somewhere. I can now finally use my SWEP and I get this error in the console.

[ERROR] addons/one piece swep/lua/weapons/weapon_second_gear/shared.lua:41: attempt to call a nil value

  1. unknown - addons/one piece swep/lua/weapons/weapon_second_gear/shared.lua:41

Timer Failed! [Simple][@addons/one piece swep/lua/weapons/weapon_second_gear/shared.lua (line 41)]



function SWEP:Deploy()
		self:EmitSound( DeploySound2 )
		timer.Simple(0.5, function() (self.Owner:SetRunSpeed(999)) (self.Owner:SetWalkSpeed(999)) end)
end