(TTT) Make a player specific variable

So, my code has a variable that disallows/allows you to execute what’s in the function. The problem is, this variable is shared throughout all players on the server.

When someone executes the function, canPickUpThatCan turns false for EVERYONE. I don’t want this.



local canPickUpThatCan = 1
function SWEP:SecondaryAttack()
	if canPickUpThatCan == 1 then
		local soundnumber = math.Round(math.random(1,3))
		if SERVER then
			local canEnt = ents.Create("prop_physics")
			
			if IsValid(canEnt) then
				local plyModel = self.Owner:GetModel()
				
				canEnt:SetModel("models/props_junk/popcan01a.mdl")
				canEnt:SetPos(self.Owner:EyePos())
				canEnt:Spawn()
				
				canEnt:GetPhysicsObject():SetVelocity(self.Owner:GetForward() * 500)
				
				timer.Simple(60, function() canEnt:Remove() end )
				
				self.Owner:SetModel("models/player/police.mdl")
				timer.Simple(1.5, function() self.Owner:SetModel(plyModel) end )
			end
		end
		
		canPickUpThatCan = 0
		timer.Simple(3, function() canPickUpThatCan = 1 end )
		if soundnumber == 1 then
			self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan1.wav")
		elseif soundnumber == 2 then
			self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan2.wav")
		else
			self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan3.wav")
		end
	end
end


self.Owner.canPickUpThatCan. Also, just use true/false instead of 0/1.

I get this error:



[ERROR] gamemodes/terrortown/entities/weapons/weapon_ttt_stunbaton.lua:221: unexpected symbol near '.'
  1. unknown - gamemodes/terrortown/entities/weapons/weapon_ttt_stunbaton.lua:0


New code:



local self.Owner.canPickUpThatCan = true
function SWEP:SecondaryAttack()
	if self.Owner.canPickUpThatCan then
		local soundnumber = math.Round(math.random(1,3))
		if SERVER then
			local canEnt = ents.Create("prop_physics")
			
			if IsValid(canEnt) then
				local plyModel = self.Owner:GetModel()
				
				canEnt:SetModel("models/props_junk/popcan01a.mdl")
				canEnt:SetPos(self.Owner:EyePos())
				canEnt:Spawn()
				
				canEnt:GetPhysicsObject():SetVelocity(self.Owner:GetForward() * 500)
				
				timer.Simple(60, function() canEnt:Remove() end )
				
				self.Owner:SetModel("models/player/police.mdl")
				timer.Simple(1.5, function() self.Owner:SetModel(plyModel) end )
			end
		end
		
		self.Owner.canPickUpThatCan = false
		timer.Simple(3, function() self.Owner.canPickUpThatCan = true end )
		if soundnumber == 1 then
			self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan1.wav")
		elseif soundnumber == 2 then
			self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan2.wav")
		else
			self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan3.wav")
		end
	end
end


doing this
[lua]
local self.Owner.canPickUpThatCan = true
[/lua]
does nothing self and self.Owner are nil outside of SWEP functions

you must make it when it Initialize’s
[lua]
function SWEP:Initialize()
self.Owner.canPickUpThatCan = true
end
[/lua]

New code:



function SWEP:Initialize()
	self.Owner.canPickUpThatCan = true
end

function SWEP:SecondaryAttack()
	if self.Owner.canPickUpThatCan then
		local soundnumber = math.Round(math.random(1,3))
		if SERVER then
			local canEnt = ents.Create("prop_physics")
			
			if IsValid(canEnt) then
				local plyModel = self.Owner:GetModel()
				
				canEnt:SetModel("models/props_junk/popcan01a.mdl")
				canEnt:SetPos(self.Owner:EyePos())
				canEnt:Spawn()
				
				canEnt:GetPhysicsObject():SetVelocity(self.Owner:GetForward() * 500)
				
				timer.Simple(60, function() canEnt:Remove() end )
				
				self.Owner:SetModel("models/player/police.mdl")
				timer.Simple(1.5, function() self.Owner:SetModel(plyModel) end )
			end
		end
		
		self.Owner.canPickUpThatCan = false
		timer.Simple(3, function() self.Owner.canPickUpThatCan = true end )
		if soundnumber == 1 then
			self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan1.wav")
		elseif soundnumber == 2 then
			self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan2.wav")
		else
			self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan3.wav")
		end
	end
end


The sound works…the can doesn’t spawn though.

remove the IsValid() check if/then block, because I’m pretty sure the can isn’t valid until you call Spawn() on it (not sure if I’m right but it might work)
[lua]function SWEP:Initialize()
self.Owner.canPickUpThatCan = true
end

function SWEP:SecondaryAttack()
if self.Owner.canPickUpThatCan then
local soundnumber = math.Round(math.random(1,3))
if SERVER then
local canEnt = ents.Create(“prop_physics”)

		local plyModel = self.Owner:GetModel()
			
		canEnt:SetModel("models/props_junk/popcan01a.mdl")
		canEnt:SetPos(self.Owner:EyePos())
		canEnt:Spawn()
		
		canEnt:GetPhysicsObject():SetVelocity(self.Owner:GetForward() * 500)
			
		timer.Simple(60, function() canEnt:Remove() end )
			
		self.Owner:SetModel("models/player/police.mdl")
		timer.Simple(1.5, function() self.Owner:SetModel(plyModel) end )
	end
	
	self.Owner.canPickUpThatCan = false
	timer.Simple(3, function() self.Owner.canPickUpThatCan = true end )
	if soundnumber == 1 then
		self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan1.wav")
	elseif soundnumber == 2 then
		self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan2.wav")
	else
		self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan3.wav")
	end
end

end[/lua]
:suicide:

Well, I mean, the can spawned before I made the variable have self.Owner attached to it.

What do you mean by that?

In my previous code when it wasn’t self.Owner.canPickUpThatCan and just canPickUpThatCan the can would spawn perfectly. The only problem with my original code is that the variable was shared between the whole server.

Nothing’s changed in the SecondaryAttack function except the variable, so I wouldn’t understand how removing the IsValid() check would help it. Of course, I’d be willing to try it, but I don’t have the means to right now.

Besides, the IsValid() check can prove true without the actual spawning of the can. In fact, I find it to be necessary because I think errors would pop up in console if there is no entity named canEnt and the code tries to set a model for it.

just do something like
[lua]
if self.Owner.canPickUpThatCan then
self.Owner:PrintMessage(HUD_PRINTTALK, “self.Owner.canPickUpThatCan is true”)
else
self.Owner:PrintMessage(HUD_PRINTTALK, “self.Owner.canPickUpThatCan is false for some reason”)
[/lua]
then do some debugging.
also
[lua]local canPickUpThatCan = 1[/lua]
is shared between the whole server because there is not player object connected to it



function SWEP:SecondaryAttack()
	if self.Owner.cantCan then return end
		
	if SERVER then

		local canEnt = ents.Create("prop_physics")			
		canEnt:SetModel("models/props_junk/popcan01a.mdl")
		canEnt:SetPos( self.Owner:EyePos() )
		canEnt:Spawn()
		
		local phys = canEnt:GetPhysicsObject()
		if IsValid(phys) then
			phys:SetVelocity(self.Owner:GetForward() * 500)
		end
		
		timer.Simple(60, function() if IsValid(canEnt) then canEnt:Remove() end end)
		
		local oldModel = self.Owner:GetModel()
		self.Owner:SetModel("models/player/police.mdl")

		timer.Simple(1.5, function() if IsValid(self.Owner) then self.Owner:SetModel(oldModel) end end)

	end
	
	self.Owner.cantCan = true
	timer.Simple(3, function() self.Owner.cantCan = nil end )

	self.Weapon:EmitSound( "npc/metropolice/vo/pickupthecan" .. math.random( 1, 3 ) .. ".wav" )

end


Here, this might help.

This worked. Thank you so much!

Just one thing…

This…



self.Weapon:EmitSound( "npc/metropolice/vo/pickupthecan" .. math.random( 1, 3 ) .. ".wav" )


Needs to be this…



self.Weapon:EmitSound( "npc/metropolice/vo/pickupthecan" .. math.Round(math.random( 1, 3 )) .. ".wav" )