I have variables that are toggled for a SWEP, but when 2 people are using the weapon, they share the variables' state. (If one player toggles, both players get toggled)
Here's the code in its entirety, excuse the mess.
[CODE]SWEP.PrintName = "Sonic SWEP"
SWEP.Author = "Corn"
SWEP.Instructions = "Mouse1 to toggle sonic mode \n Mouse2 to taunt"
SWEP.Category = "Corn's Guns"
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
SWEP.ViewModel = ""
SWEP.WorldModel = "models/weapons/w_hands.mdl"
SWEP.Primary.ClipSize = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"
local speedmod = false
local jumpcd = 0
function SWEP:PrimaryAttack()
ply = self.Owner;
self.Weapon:SetNextPrimaryFire( CurTime() + 3 )
ply.speedmod = !ply.speedmod
if ply.speedmod == true then
self.Owner:EmitSound("weapons/spindash.wav")
self.Owner:SetRunSpeed(1500)
self.Owner:SetWalkSpeed(900)
self.Owner:SetJumpPower(500)
self.Owner:SetFOV(120, 1)
elseif ply.speedmod == false then
self.Owner:SetRunSpeed(400)
self.Owner:SetFOV(90, 0.5)
self.Owner:SetWalkSpeed(200)
self.Owner:SetJumpPower(200)
end
end
function SWEP:SecondaryAttack()
self.Weapon:SetNextSecondaryFire(CurTime() + 3)
self.Owner:EmitSound("weapons/taunt.wav")
end
function SWEP:Holster()
self.Owner:SetRunSpeed(400)
self.Owner:SetFOV(90, 0.5)
self.Owner:SetWalkSpeed(200)
self.Owner:SetJumpPower(200)
speedmod = false
return true
end
function SWEP:Think()
if self.Owner:KeyPressed(IN_JUMP) && speedmod == true && ( jumpcd + 5 <= CurTime() ) then
self.Owner:EmitSound("weapons/jump.wav")
jumpcd = CurTime()
end
end
end
function SWEP:GetFallDamage(pl, speed)
if self.Owner == pl and self.Owner:GetActiveWeapon() == self then
return 0
end
end
function SWEP:Initialize()
if SERVER then
hook.Add ("GetFallDamage", self, self.GetFallDamage)
end
end[/CODE]
I'd like to make it so "speedmod" is player-specific. (I think it's a global variable right now?)
[code]
-- Outside of any methods
SWEP.speedmod = false
-- Inside any methods
self.speedmod = true
self.speedmod = false
if self.speedmod then
else
end
etc...
[/code]
If you want access to your variable clientside as well, look in to [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/NetworkVar]Entity:NetworkVar[/url]
[QUOTE=Willox;46704482][code]
-- Outside of any methods
SWEP.speedmod = false
-- Inside any methods
self.speedmod = true
self.speedmod = false
if self.speedmod then
else
end
etc...
[/code][/QUOTE]
So adding self. to the variable will accomplish this?
Looking at your code, you're going to have to be networking the variable for clientside prediction to work properly in PrimaryAttack.
I really recommend using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/NetworkVar]Entity:NetworkVar[/url] if you want your weapon to work properly in multiplayer.
[QUOTE=Willox;46704519]Looking at your code, you're going to have to be networking the variable for clientside prediction to work properly in PrimaryAttack.
I really recommend using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/NetworkVar]Entity:NetworkVar[/url] if you want your weapon to work properly in multiplayer.[/QUOTE]
[CODE]function SWEP:SetupDataTables()
self:NetworkVar("Bool", 0, "speedmod")
end
function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire( CurTime() + 3 )
self.speedmod = !self.speedmod
if self.speedmod == true then
self.Owner:EmitSound("weapons/spindash.wav")
self.Owner:SetRunSpeed(1500)
self.Owner:SetWalkSpeed(900)
self.Owner:SetJumpPower(500)
self.Owner:SetFOV(120, 1)
elseif self.speedmod == false then
self.Owner:SetRunSpeed(400)
self.Owner:SetFOV(90, 0.5)
self.Owner:SetWalkSpeed(200)
self.Owner:SetJumpPower(200)
end
end[/CODE]
?
You'd then use self:Getspeedmod() and self:Setspeedmod(value).
The example is quite specific.
All of this was explained in your original topic. Why create another?
Regardless, this helped me out as well, lol!
Sorry, you need to Log In to post a reply to this thread.