How do i made a weight inventory system?

I want to add weight to tools and maximum weight that inventory can hold, so player can hold only specific amount of items depending on weight of each one picked up. For example player picked up tool by using proximity prompt and it has weight of 4, maximum inventory weight is 5, so they can pickup items only which weight 1 or less.
Just like in game “Isle”

I havent found any answers or resources to make that, so i need good explaination of every script i would need to write and how would i place scripts, because i cannot script by my self without doing tons of search and running into lots of bugs and issues, and also havent tried to edit scripts from toolbox because theres only few which let you pickup items other than touching them, they look messy, have almost 0 explaination and also i dont know how to properly edit them to implement limit of weight.

1 Like

I would add two NumberValues to the player via Instance.new(). One of them would be their current carrying weight, and the other would be the maximum they can carry.
Then for every tool in your game, add a NumberValue to it named “ToolWeight” and give it whatever number you find best.
In a ChildAdded and ChildRemoved event for the player’s backpack (In a server script. I would put it in StarterPlayerScripts), you can add or remove the value from the NumberValue inside of that tool onto the “current weight” NumberValue in the respective event.

How do you handle item cloning to player backpack? I have an example but I don’t know how you handle item cloning? Well I don’t know you clone items but I made one for you.

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	local MaxWeight = Instance.new("IntValue")
	MaxWeight.Name = "MaxWeight"
	MaxWeight.Value = 25
	MaxWeight.Parent = Player
	local Weight = Instance.new("IntValue")
	Weight.Name = "Weight"
	Weight.Value = 0
	Weight.Parent = Player
	Player.CharacterAdded:Connect(function(Character)
		local Backpack = Player.Backpack
		Backpack.ChildAdded:Connect(function(Child)
			if Child:IsA("Tool") then
				Player.Weight.Value += Child:GetAttribute("Weight")
			end
		end)
		Backpack.ChildRemoved:Connect(function(Child)
			if Child:IsA("Tool") then
				if Character:FindFirstChild(Child.Name) then -- Player just equipped it.
					return
				end
				Player.Weight.Value -= Child:GetAttribute("Weight")
			end
		end)
	end)
end)
function CloneItemAndGivetoPlayer(Player:Player?, Tool:Tool?)
	if Player.Weight.Value <= Player.MaxWeight.Value or Player.Weight.Value ~= Player.MaxWeight.Value then -- Maximum weight threshold not reached yet.
		Tool:Clone().Parent = Player.Backpack
	else
		warn("Maximum weight! Cannot give anymore tools!")
	end
end

Make sure to set the tool a number attribute with the value being its weight.
It would work like this:

local Prom:ProximityPrompt? = workspace.ProximityPrompt
Prom.TriggerEnded:Connect(function(Player)
	if Player.Character and Player.Character:FindFirstChildOfClass("Humanoid").Health > 0 then
		CloneItemAndGivetoPlayer(Player, game:GetService("ServerStorage").Tools.Sword)
	end
end)

Thx! ill try that now. Ill tell if everything worked fine, if not ill just tell you the problem.

1 Like

Actually im not sure about where to place second script, frist script i placed in starter player scripts, and second im not sure where to exactly.

The first script should be a serverscript inside serverscriptservice.
The proximity prompt one should be in the same script too.

To implement a weight system for your game, you will need to create a script that tracks the weight of the player’s inventory and limits the items they can pick up based on their weight. Here are the basic steps you can follow:

    1. First, you will need to add a weight property to each item you want to include in the system. You can do this by adding a NumberValue or IntValue to each tool in the game. For example, you could add a “Weight” value of 4 to a tool that weighs 4 units.
    1. Next, you will need to create a script that tracks the weight of the player’s inventory. You can do this by creating a new script and attaching it to a player’s character or backpack.
    1. In the script, create a variable to keep track of the current weight of the player’s inventory. When an item is picked up, add its weight value to this variable. When an item is dropped or removed from the inventory, subtract its weight value.
    1. Modify your pickup script to check the weight of each item before adding it to the player’s inventory. If the weight of the item plus the current weight of the player’s inventory exceeds the maximum weight limit, then the item should not be picked up.
    1. You can display the player’s current inventory weight and maximum weight limit in the game interface. You can also create a message or sound effect when the player tries to pick up an item that is too heavy.

Here is some sample code to get you started:

-- Define maximum weight limit for the player's inventory
local MAX_WEIGHT = 5

-- Track the player's current inventory weight
local inventoryWeight = 0

-- Function to check if an item can be picked up based on its weight
local function canPickupItem(item)
    local itemWeight = item:FindFirstChild("Weight")
    if not itemWeight then
        return true -- If the item doesn't have a weight value, allow pickup
    end
    return inventoryWeight + itemWeight.Value <= MAX_WEIGHT
end

-- Connect a function to the tool's Equipped event
tool.Equipped:Connect(function()
    if canPickupItem(tool) then
        -- Add the tool's weight to the player's inventory
        local toolWeight = tool:FindFirstChild("Weight")
        if toolWeight then
            inventoryWeight = inventoryWeight + toolWeight.Value
        end
    else
        -- Display a message or sound effect indicating that the item is too heavy
        print("You cannot pick up this item because it is too heavy.")
    end
end)

-- Connect a function to the tool's Unequipped event
tool.Unequipped:Connect(function()
    -- Subtract the tool's weight from the player's inventory
    local toolWeight = tool:FindFirstChild("Weight")
    if toolWeight then
        inventoryWeight = inventoryWeight - toolWeight.Value
    end
end)

This is just a basic example to get you started, and you may need to modify or add more code depending on the specific needs of your game. Additionally, it is important to thoroughly test your weight system to ensure that it is working as intended and that players cannot exploit it.

So i added second script in frist one, as you said, now what about serverstorage.tools.sword?
Am i supposed to create tool in server storage, name it sword and clone few of it into workspace?

It’s just an eaxmple folder, replace serverstorage.tools.sword with your folder that has tools.

So i just tested and it didnt work, limit was reached cause i got 6 tools picked up one by one, each having Weight attribute set to 5, while maximum is 25 as i seen in script.

I’m confused, were you able to pick anymore items or no?

Yeah i can continue picking up items.
изображение


What is the Tool in ServerStorage weight? Plus you need to handle every tool picking up in that function in the serverscript.

Tool in workspace and in serverstorage are the same

But are the tools cloned using the function in the server-script?
Hold on lemme do a rewrite.

Tools are picked up by default picking up function. They dont have proximity prompt, and dont delete and clone in backpack.

function CloneItemAndGivetoPlayer(Player:Player?, Tool:Tool?)
	if Player.Weight.Value ~= Player.MaxWeight.Value then -- Maximum weight threshold not reached yet.
		Tool:Clone().Parent = Player.Backpack
	else
		warn("Maximum weight! Cannot give anymore tools!")
	end
end

Still doesnt work right, i might positioned something not right, or code have bugs, idk.
I might need .rbx file of place with these done so i can see how script and tools must be positioned.

How do you do the picking up? I used my snippet and it works…

As i said, im just walking into tool and it picks up, proximity prompt doesnt appear.