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)