Getting weight of all players weapons

So I am trying to make a system that modifies the players speed depending on the weight of all the weapons he is carrying for my gamemode.
So if the player has the following:

RPG: 75
USP: 25
Knife: 10

It makes the players walk and runspeed 110 less.
I cant figure out how to do this tho, only how to get the weight of the weapon the player is holding. Any ideas?

Player:GetWeapons

Weapon:GetWeight
Add them all in a for loop

Not sure if GetWeight actually gets the weight though, it might be for something else

Weight refers to the weapon’s auto-switch priority. You can see the Source implementation here: https://github.com/Kefta/GSWeapons/blob/master/gsweapons_base/lua/code_gs/gsweapons_gslib.lua#L78-L114

Yep I know that, but I use it for actual weight. :stuck_out_tongue:

That’s going to be pretty inaccurate.

Not at all, I love weapons having the weight = amount of damage they do, or really any random value.

Or you know, you could create an addon that creates a weight value for all the weapons and works with that value instead. Here is a tiny example I tried writing last night, doubt it works at the moment but you get the gist.


--serverside

hook.Add("PlayerInitialSpawn", "initWeight", function(ply)
	ply.MaxWeight = 100; --here you can change the max weight the player can hold
	ply.CurrWeight = 0;
	ply:setWeightofWeapons() --setting weight of default weapons
end)

hook.Add("Initialize", "checkWeightRunSpeed", function()
	timer.Create("checkWeightchangeSpeeds", 1, 0, function() --adjusting movement speed
		local players = player.GetAll();
		local runspeed = 500; --base gamemode runspeed
		local walkspeed = 200; --base gamemode walkspeed
		for _, v in pairs(players) do
			v.MaxWeight = v.MaxWeight or 100;
			v.CurrWeight = v.CurrWeight or 0;
			if IsValid(v) then
				if v:getWeightofWeapons() >= 100 then
					v:SetRunSpeed(200) --reducing speeds here
					v:SetWalkSpeed(50)
				else
					v:SetRunSpeed(runspeed)
					v:SetWalkSpeed(walkspeed)
				end
			end
			print(v:getWeightofWeapons())
		end
	end)
end)

hook.Add("PlayerCanPickupWeapon", "checkWeight", function(ply, wep)
	wep.WeaponWeight = wep.WeaponWeight or 10;
	if (ply:getWeightofWeapons() + wep.WeaponWeight) <= ply.MaxWeight then --prevent the player from picking up weapons that put them over their weight limit
		return true
	else
		return false
	end
end)

local p = FindMetaTable("Player");

function p:setWeightofWeapons()
	for k, v in pairs(self:GetWeapons()) do
		if v.WeaponWeight == nil then
			v.WeaponWeight = 0 --sets weapons to default 0 weight
			self:adjustWeight(v.WeaponWeight)
		end
	end
end

function p:getWeightofWeapons()
	for k, v in pairs(p:GetWeapons()) do
		local w = v.WeaponWeight;
		w = w + v.WeaponWeight
	end
	self.CurrWeight = w;
	return self.CurrWeight
end

Awesome, thanks!