[Lua/Help] Grabbing the SWEP.Weight Values of All Weapons Currently Held By Player and Returning an Integer

How would I go about running through all the weapons the player has, and grabbing the SWEP.Weight values off of each of them and adding that into an integer?

I also need to be able to dynamically update that integer in case the player drops one of the weapons.

I know about Weapon:GetWeight(), but I’m not sure how to make a list of all the weapons the player has and grabbing the weights off them at the same time.

I just need to know the right commands to use, and probably some pseudocode, and that will get me rolling.

Here’s what I have so far:

[lua]

if !ConVarExists(“VNT_STALKERHUD_Weight_Toggle”) then CreateConVar( “VNT_STALKERHUD_Weight_Toggle”, 1, { FCVAR_ARCHIVE, FCVAR_REPLICATED } ) end
if !ConVarExists(“VNT_STALKERHUD_Weight_Max”) then CreateConVar( “VNT_STALKERHUD_Weight_Max”, 40, { FCVAR_ARCHIVE, FCVAR_REPLICATED } ) end

[…]

	if (ConVarExists( "VNT_STALKERHUD_WeightToggle" ) and GetConVar( "VNT_STALKERHUD_WeightToggle" ) != 0 ) and ConVarExists("VNT_STALKERHUD_Weight_Max") then
	
		local _P = LocalPlayer()
		local _W = _P:GetActiveWeapon()	
		local _WKG = _W:GetWeight()
		
		local _TBL = _P:GetWeapons()

– run through all weapons held by player once per 2 seconds

– grab weight of all weapons

– send weight to NWInt

– calculate weight based on weight:GetNWInt() then slow down player based on weight

– else if player is over GetConVarNumber(“VNT_STALKERHUD_Weight_Max”) then slow player to 0.1 of their speed

		print( _WKG )
	end

[/lua]

Am I on the right track?

Yeah you are on the right track as long as all of your pseudo code is serverside. You can only set NW variables on the server, for one. And it would be more secure if you did the weight calculations on the server because they could be tampered with otherwise.

With regards to getting the weight, you would want to create a timer

timer.Create that runs every 2 seconds for an infinite amount of time. Then you’d want to use an ‘in pairs’ loop and iterate through all of the players weapons adding their weight to a variable

You could also just cache the weights and modify the table when the player either picks up or drops a weapon, and then sum that table in the same block.

I have no idea how to cache the weapons the player has or grab a table of all of them. I have this right now:

[lua]
if SERVER then

local _P = debug.getregistry().Player	--	Player
local _ENT = debug.getregistry().Entity	--	Entity

if ( _P:Alive() and _P:IsValid() ) then	-- Safety Checks
	local _TIMERRATE = GetConVarNumber("VNT_STALKERHUD_Weight_UpdateRate" )	--	The update rate CVar
	local _ACTIVEWEP = _P:GetActiveWeapon()	--	The active weapon
	local _WEAPONS = _P:GetWeapons()
	local _CURWEIGHT = 0
	timer.Create( _TIMERRATE, 0, function()
		for _KEY, _WEAPONS in pairs _P do
			if ( _WEAPONS.Weight:IsValid() ) then
				_CURWEIGHT = _CURWEIGHT + _WEAPONS.Weight
				_P:SetNWInt( "VNTSTALKERHUDWeightCurrent", _CURWEIGHT )
			end
		end
	end)
end

end
[/lua]

It’s returning 0 as the NWInt now, so I don’t think the loop is running. Any ideas?

I added a print and the whole timer isn’t running.



local _P = debug.getregistry().Player	--	Player
	local _ENT = debug.getregistry().Entity	--	Entity

I dont think you understand what these two lines do.

What they don’t do is anything to do with actual players.

[editline]23rd January 2016[/editline]

Your code is all completely wrong and makes 0 sense.

I’m a mapper, not a coder.

If I knew how to code it right I wouldn’t be here asking for help.

I need a way to call each player individually on the server so I can set their weights.

for k, v in pairs( player.GetAll() ) do end ?

I wouldn’t rely on SWEP.Weight because noone ever sets the value to anything other than 5.

Here’s what I’ve got now, but the timer is failing because the GetWeapons() command refuses to work for some reason.

[lua]
if SERVER then
timer.Create( “VNTSTALKERHUDWeightTimer_SV”, 1, 0, function()
local _P = player.GetAll()
local _WEAPONS = _P:GetWeapons()
local _CURWEIGHT = 0
for _KEY, _VALUE in pairs(_P) do
_CURWEIGHT = _CURWEIGHT + _WEAPONS.Weight
_P:SetNWInt( “VNTSTALKERHUDWeightCurrent”, _CURWEIGHT )
end
end)
end
[/lua]

I’m calling GetWeapons() on a player, but it’s not working; any idea why?

[editline]22nd January 2016[/editline]

That’s not the point. I set the weight on my weapons and that’s what this is designed to work with.

GetWeapons returns a number, how many weapons the player has.
You are also trying to Get the weapons of a table ( player.Getall() )

What the fuck are you even talking about?



if SERVER then
	timer.Create( "VNTSTALKERHUDWeightTimer_SV", 1, 0, function()
		for _, ply in pairs( player.GetAll() ) do
			local _CURWEIGHT = 0
			for _, w in pairs( ply:GetWeapons() ) do _CURWEIGHT = _CURWEIGHT + ( w.Weight or 0 )  end
			ply:SetNWInt( "VNTSTALKERHUDWeightCurrent", _CURWEIGHT )
		end
	end)
end

Just take the code.