SWEP only works for one person at a time in Multiplayer

Hello,
Fledgling developer here.

I am currently working on a Jetpack SWEP and I have run into a strange issue that I can’t make any sense of.

In Singleplayer, the SWEP works just fine but in Multiplayer if two or more people have it equipped, it will only work for one person at a time.

EG: A friend joins my server and uses the jetpack to fly, then me (The Host) equips it and begins to fly as well: friend’s jetpack will no longer work until I stop using mine.

I’ve spent a good handful of hours trying to figure this out but I just have not gotten anywhere, I must be doing something wrong but perhaps i’m blind to it.

Here is the code for flight.

[LUA]
function SWEP:Think()

if !self.Owner:IsValid() or self.Owner == nil then return end 

if !self.Sound then 
		self.Sound = CreateSound(self.Owner, "ambient/gas/cannister_loop.wav")
end

if !self.Sound2 then 
		self.Sound2 = CreateSound(self.Owner, "ambient/gas/steam_loop1.wav")
		self.Sound2:Play()
		self.Sound2:ChangeVolume(0,0)
end 


if self.Owner:KeyReleased(IN_JUMP) then 
	self.Sound:ChangeVolume(0,0.3)

	timer.Simple(0.4, function()
		if self.Sound then self.Sound:Stop() end 			
	end)
		
end 

if self.Owner:KeyDown(IN_JUMP) then 
	if self.Pressure > 0 then 

		self.Sound:Play()
		self.Sound:ChangeVolume(0.1, 1)

		self.Sound2:ChangeVolume(0,0)
	
		
		
		if CurTime() < delay then return end 
		delay = CurTime() + 0.1
		
		rechargewait = CurTime() + 2

		self.Owner:SetVelocity(self.Owner:EyeAngles():Up() * 120)
		self.Pressure = math.Clamp(self.Pressure -1, 0, 100)

		if CLIENT then 

			local back = self.Owner:LookupBone("ValveBiped.Bip01_Spine2")
			local backpos = self.Owner:GetBonePosition(back)
			local emitter = ParticleEmitter(self.Owner:GetPos())
			for i = 1, 3 do 

				local p = emitter:Add("effects/bubble", backpos + self.Owner:EyeAngles():Up() * -30 + self.Owner:EyeAngles():Forward() * -5)

				if p then 
				p:SetDieTime(2)
				p:SetStartAlpha(255)
				p:SetEndAlpha(0)
				p:SetStartSize(5)
				p:SetEndSize(0)
				p:SetRoll(2)
				p:SetRollDelta(math.random(2,5))
				p:SetVelocity((self.Owner:EyeAngles():Up() * -90) + (self.Owner:EyeAngles():Right() * math.random(-25,25)) + self.Owner:EyeAngles():Forward() * math.random(-25,25))
				end 
			end 

			for i =1,4 do
				local p = emitter:Add("particle/smokesprites_000".. math.random(1,9), backpos + self.Owner:EyeAngles():Up() * -30 + self.Owner:EyeAngles():Forward() * -5)
				if p then 
				p:SetDieTime(2)
				p:SetStartAlpha(150)
				p:SetEndAlpha(0)
				p:SetStartSize(15)
				p:SetEndSize(0)
				p:SetRoll(2)
				p:SetRollDelta(math.random(2,5))
				p:SetVelocity((self.Owner:EyeAngles():Up() * -20) + (self.Owner:EyeAngles():Right() * math.random(-50,50)) + self.Owner:EyeAngles():Forward() * math.random(-50,50))
				end 

			end 

			for i =1,5 do
				local p = emitter:Add("particle/rain", backpos + self.Owner:EyeAngles():Up() * -30 + self.Owner:EyeAngles():Forward() * -5)
				if p then 
				p:SetDieTime(2)
				p:SetStartAlpha(150)
				p:SetEndAlpha(0)
				p:SetStartSize(7)
				p:SetEndSize(0)
				p:SetRoll(2)
				p:SetRollDelta(math.random(2,5))
				p:SetVelocity((self.Owner:EyeAngles():Up() * -40) + (self.Owner:EyeAngles():Right() * math.random(-50,50)) + self.Owner:EyeAngles():Forward() * math.random(-50,50))
				end 

			end 

			emitter:Finish()
		end  
	end 
end 


	if rechargewait < CurTime() then
		if self.Pressure < 100 then 

			self.Sound2:ChangeVolume(0.25, 0.2)
			self.Pressure = math.Clamp(self.Pressure +1, 0, 100)
			rechargewait = CurTime() + 0.05
		else
			self.Sound2:ChangeVolume(0, 0.2)	
		end
	end 


	self.VElements["R Needle Base"].angle.y = self.Pressure * 2.6 - 132	

end

[/LUA]

Another thing to note is on the weapon’s viewmodel is a pressure gauge set to (self.Pressure) defined by SWEP.Pressure. When the bug occurs, my pressure gauge seems to try and indicate the other player’s pressure instead of mine.

I’m assuming I’m having an issue getting only the owner of the SWEP in particular rather than every player who owns the SWEP. Is self.Owner not the way to go?

Thank you for your time.
-Azbr.

Update:
I discovered the offending line of code that is causing this bug, but not the reason why it’s doing this.

[lua]

if self.Owner:KeyDown(IN_JUMP) then 
	if self.Pressure > 0 then 

		self.Sound:Play()
		self.Sound:ChangeVolume(0.1, 1)

		self.Sound2:ChangeVolume(0,0)
		
		if CurTime() < delay then return end  -- <<< It's this line right here <<<
		delay = CurTime() + 0.1
		self.Owner:SetVelocity(self.Owner:EyeAngles():Up() * 120)
		
		rechargewait = CurTime() + 2

		self.Pressure = math.Clamp(self.Pressure -1, 0, 100)

[/lua]

If CurTime() < delay then return end

For some reason, this particular line of code causes the SWEP to only work for one person at a time and I cannot figure out why this is. Simply removing it will undo any control over the flight ability too.

Can anybody give me a clue about this?

-Azbr

Where is the “delay” variable defined?

Replace the two lines with this instead:

if CurTime() < (self.delay or 0) then return end
self.delay = CurTime() + 0.1

“delay” was defined as a local variable of “0” right outside of the SWEP:Think() function, as was “rechargewait”.

But following what you put, I changed both delay and rechargewait to swep.delay and swep.rechargewait and that looks like it did it!

Thank you very much.

I’m assuming setting those variables as local variables outside of any function somehow made them apply to everyone using the weapon.

Yeah those variables would have been global for every swep seeing as the variable is defined there for every swep not just for each instance of the swep if that makes sense.

Ahh,
That makes a whole lotta sense.
Never woulda crossed my mind.

I still have a hell of a lot to learn.

Thanks once again.

1 Like