I have made a weighted chance system. In the script I have made a table where it determines the name of an item, the weight and the cost of that item.
And if the proxpromt is triggered it will give a random item. Which works good
And in another function, I have made it so, it prints the item name and the cost and it creates a tool in the player’s backpack.
HOWEVER!!! the cost that it prints is always 70, which is the Broken furniture’s cost.
Also I will add alot for items to the table
Any solutions?
Script:
player = game:GetService("Players").PlayerAdded:Wait()
local items = {
{name = "Newspaper", weight = 20, cost = 3},
{name = "Rat", weight = 20, cost = 2},
{name = "Broken furniture", weight = 10, cost = 70}
}
local function getRandomItem()
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
end
end
end
local function getRandomCost()
local totalcost = 0
for _, item in items do
totalcost = totalcost + item.cost
end
local randomCost = totalcost
local currentCost = 0
for _, item in items do
currentCost = currentCost + item.cost
if randomCost <= currentCost then
return item.cost
end
end
end
local function onProximityPromptTriggered()
local selectedItem = getRandomItem()
local selectedCost = getRandomCost()
print("Player received: " .. selectedItem)
print("Item is worth: " .. selectedCost)
local tool = Instance.new("Tool")
tool.Name = selectedItem
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Size = Vector3.new(1, 1, 1)
handle.Parent = tool
tool.Parent = player.Backpack
end
local dumpster = script.Parent
local proximityPrompt = dumpster:FindFirstChildOfClass("ProximityPrompt")
if proximityPrompt then
proximityPrompt.Triggered:Connect(onProximityPromptTriggered)
end
Also, one question, is the weight in the weighted chance system to a percent?Like how newspaper’s weight is 20 is that a 20% chance?
player = game:GetService("Players").PlayerAdded:Wait()
local items = {
{name = "Newspaper", weight = 20, cost = 3},
{name = "Rat", weight = 20, cost = 2},
{name = "Broken furniture", weight = 10, cost = 70}
}
local function getRandomItem()
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
end
end
end
local function getRandomCost()
local totalcost = 0
for _, item in items do
totalcost = totalcost + item.cost
end
local randomCost = math.random(0, totalcost)
local currentCost = 0
for _, item in items do
currentCost = currentCost + item.cost
if randomCost <= currentCost then
return item.cost
end
end
end
local function onProximityPromptTriggered()
local selectedItem = getRandomItem()
local selectedCost = getRandomCost()
print("Player received: " .. selectedItem)
print("Item is worth: " .. selectedCost)
local tool = Instance.new("Tool")
tool.Name = selectedItem
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Size = Vector3.new(1, 1, 1)
handle.Parent = tool
tool.Parent = player.Backpack
end
local dumpster = script.Parent
local proximityPrompt = dumpster:FindFirstChildOfClass("ProximityPrompt")
if proximityPrompt then
proximityPrompt.Triggered:Connect(onProximityPromptTriggered)
end
Try this instead, where I combined both getRandomItem() and getRandomCost(). The only difference from the change was adding item.cost to the end of return item.name, and tweaking onProximityPromptTriggered() to adapt to the change.
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 function onProximityPromptTriggered()
local selectedItem, selectedCost = getRandomItemInfo()
print("Player received: " .. selectedItem)
print("Item is worth: " .. selectedCost)
local tool = Instance.new("Tool")
tool.Name = selectedItem
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Size = Vector3.new(1, 1, 1)
handle.Parent = tool
tool.Parent = player.Backpack
end
``