How to define the totalitemcost

Hello!

In my script I have made a variable called TotalItemCost.

This is basiclly the cost of everything in the player’s backpack added up.

And what I want to do is, loop through the player’s backpack and I have made a table where it shows the weight (I am using a weighted chance system) and the cost of an item.

Then it adds the cost of the item all together and thats the TotalItemCost’s value.

Except I don’t know where to put it.

Full script:

player = game:GetService("Players").PlayerAdded:Wait() 


local RepliStorage = game:GetService("ReplicatedStorage")


local items = { -- Here is the table with all the item values
	{name = "Newspaper", weight = 20, cost = 3},
	{name = "Rat", weight = 20, cost = 2},
	{name = "Broken Furniture", weight = 2, cost = 70},
	{name = "Toy", weight = 13, cost = 8},
	{name = "Bottle", weight = 20, cost = 3},
	{name = "Shoes", weight = 7, cost = 13},
	{name = "Monitor", weight = 2, cost = 85},
	{name = "Clothes", weight = 13, cost = 10},
	{name = "Scrap Metal", weight = 20, cost = 2},
	{name = "Cardboard", weight = 20, cost = 4},
	{name = "Book", weight = 20, cost = 4},
	{name = "Plastic Container", weight = 13, cost = 7},
	{name = "Food Scraps", weight = 13, cost = 13},
	{name = "Hammer", weight = 7, cost = 30},
	{name = "Can", weight = 20, cost = 3},
	{name = "Magazines", weight = 13, cost = 12},
	{name = "Dishes", weight = 7, cost = 27},
	{name = "Batteries", weight = 7, cost = 25},
	{name = "Desk", weight = 2, cost = 115},
	{name = "Light", weight = 2, cost = 43},
	{name = "PC", weight = 1, cost = 468},
	{name = "Fridge", weight = 1, cost = 286},
	{name = "Microwave", weight = 1, cost = 286},
	{name = "Paint Brush", weight = 13, cost = 11},
	{name = "Paint Can", weight = 13, cost = 7},
}




local function getRandomItemInfo()
	local totalWeight = 0

	for _, item in items do
		totalWeight = totalWeight + item.weight
	end

	local randomWeight = math.random(1, totalWeight)
	local currentWeight = 0

	for _, item in items do
		currentWeight = currentWeight + item.weight
		if randomWeight <= currentWeight then
			return item.name, item.cost
		end
	end
end

local Item = 0
local ItemCost = 0
local TotalItemCost = 0 -- Here is the TotalItemCost Value

local function onProximityPromptTriggered(plr)
	local selectedItem, selectedCost = getRandomItemInfo()
	print("Player received: " .. selectedItem)
	print(selectedItem.. " is worth: " .. selectedCost)

	if RepliStorage.ItemAssets:FindFirstChild(selectedItem) then
		local tool = RepliStorage.ItemAssets:FindFirstChild(selectedItem):Clone()
		tool.Parent = plr.Backpack
		Item = selectedItem
		ItemCost = selectedCost
	else
		print(selectedItem.. " is not found in ReplicatedStorage")
		local tool = Instance.new("Tool")
		tool.Parent = plr.Backpack
		tool.Name = selectedItem
		local handle = Instance.new("Part")
		handle.Parent = tool
		handle.Name = "Handle"
		handle.Size = Vector3.new(1,1,1)
		Item = selectedItem
		ItemCost = selectedCost
	end
	
end

local dumpster = script.Parent

RepliStorage.Remotes.Dumpster1.OnServerEvent:Connect(function(plr)
	onProximityPromptTriggered(plr)
end)


-- Sell Item

local SellNPCRemote = RepliStorage.Remotes.SellNPC
local SellInventoryRemote = RepliStorage.Remotes.SellIInventory

local NPCProxPromt = workspace.OminusOfficesMap.SellNPC["Hobo Henry"].Torso.ProximityPrompt

SellNPCRemote.OnServerEvent:Connect(function(plr)
	if Item ~= 0 and ItemCost ~= 0 then
		
		local tool = plr.Character:FindFirstChild(Item)
		if tool then
			plr.leaderstats.Credits.Value = plr.leaderstats.Credits.Value + ItemCost
			tool:Destroy()
			tool = 0
			TotalItemCost =- ItemCost
			ItemCost = 0
			
		else
			print("Tool could not be found:", Item)
		end
		
		
		
	else
		print("No item")
	end
end)

SellInventoryRemote.OnServerEvent:Connect(function(plr)
	if Item ~= 0 and ItemCost ~= 0 then
		local tools = plr.Backpack:GetChildren()


		local Backpack = plr.Backpack
		local Character = plr.Character
		
		
		
		for _, i in tools do
			
			print(i)
			plr.leaderstats.Credits.Value = plr.leaderstats.Credits.Value + TotalItemCost -- I'll change this part to make it give the player the value of the TotalItemCost in credits
			i:Destroy()

		end
	end
end)
1 Like

Also if you are wondering, the first part of the script gives a player a random item and the the second part sells the item when talking to an npc( which fires a remote event to this script)

And yes this is a serverscript.

1 Like

I still need help on this problem.

1 Like

If anything, one method comes down to storing the information of items that are in the inventory in a separate table along with all the information of the items. Copy the value of the picked index (items) that is given to the player and store it in said table, then wherever that information is being held, run a for.. do loop over it.


local backpack = {}
local backpackValue = 0

...

table.insert(backpack, item) --//'item' being the picked item in the 'items' list

...

backpackValue = 0 --//Reset counter before counting
for _, v in backpack do
	print(v.name, ";", v.cost)
	backpackValue += v.cost
end
print("Total backpack value:", backpackValue)

These should be placed in their respectful LocalScripts that handle the player inventory. Communication of values could be transferred with the use of RemoteEvents or similar.

1 Like

What does the backpackvalue do?

Also is the script you wrote, a server or local script?

It is a custom value that should display the final added sum of all the item.cost values in the player’s inventory.


LocalScript, as per explained in the last line of my previous post. The values this will display is dedicated towards a single player, so it would be inaccurate if everyone in the server used the same value.

1 Like

How would i do this. Like in the server script (that is mentioned in my original post) Would i need to use a remote event and have the parameters as the items in the player’s backpack.

Or do I get everything from the Items table.

Sorry, but a different and perhaps easier approach to this.


Reading a bit more carefully, you seem to have a cloning system set up, with pre-existing parts for the items. To make it easier, you can give those items in the ReplicatedStorage Attributes. You can set a Cost and Weight variable for the items which is in the original table, and use those Attributes to obtain the cost and weight for the player. Cloning these items will clone the Attribute with it.



Then you have to add LocalScript that will listen to the player’s inventory changing. Here is an example;

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local backpack = player.Backpack

local totalCost = 0
local totalWeight = 0

backpack.ChildAdded:Connect(function(item)
	print("Added " .. item.Name .. " worth:", item:GetAttribute("Cost"), item:GetAttribute("Weight"))
	totalCost, totalWeight = totalCost + item:GetAttribute("Cost"), totalWeight + item:GetAttribute("Weight")
	print("Total Cost:", totalCost, "Total Weight:", totalWeight)
end)

backpack.ChildRemoved:Connect(function(item)
	print("Removed " .. item.Name .. " worth:", item:GetAttribute("Cost"), item:GetAttribute("Weight"))
	totalCost, totalWeight = totalCost - item:GetAttribute("Cost"), totalWeight - item:GetAttribute("Weight")
	print("Total Cost:", totalCost, "Total Weight:", totalWeight)
end)

These values will both add and remove the totalCost and totalWeight of the items in the player’s backpack.

1 Like

In my serverscript I am using a weigted chance system so the weight is not actually a value.

So what would I need to change in the local script above so it is for only the item’s cost?

You mean like so?

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local backpack = player.Backpack

local totalCost = 0

backpack.ChildAdded:Connect(function(item)
	print("Added " .. item.Name .. " worth:", item:GetAttribute("Cost"))
	totalCost += item:GetAttribute("Cost")
	print("Total Cost:", totalCost)
end)

backpack.ChildRemoved:Connect(function(item)
	print("Removed " .. item.Name .. " worth:", item:GetAttribute("Cost"))
	totalCost -= item:GetAttribute("Cost")
	print("Total Cost:", totalCost)
end)
1 Like

where is the “item” from like in both functions the paramaeter is item where did you get that from?

There are a few problems.

ServerScript:
When an item is cloned, it doesn’t go in the player’s backpack

if RepliStorage.ItemAssets:FindFirstChild(selectedItem) then
		local tool = RepliStorage.ItemAssets:FindFirstChild(selectedItem)
		tool:Clone()
		tool.Parent = plr.Backpack
		tool.Name = selectedItem
		Item = selectedItem
		ItemCost = selectedCost

Local script:

  • None of the print statements print when a new tool is added to the player’s backpack.

Heart this post if you have read this message.

Hello?

Is there anyone else to help?