Hello!
I have made a script where it detects an Item the player randomly recieves and prints the item name and cost/value.
I want it to identify the item name without if statements. This is because using if statements would be long, cluttered and repetitive.
If I was using if statements it would look like this:
local function onProximityPromptTriggered()
local selectedItem, selectedCost = getRandomItemInfo()
print("Player received: " .. selectedItem)
print(selectedItem.. " is worth: " .. selectedCost)
if selectedItem == "Newspaper" then
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
elseif selectedItem == "Rat" then
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
elseif selectedItem == "Broken Furniture" then
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
-- And on and on.
end
end
Any other ways?
Full 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 = 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 function onProximityPromptTriggered() -- Here is the function I am talking about.
local selectedItem, selectedCost = getRandomItemInfo()
print("Player received: " .. selectedItem)
print(selectedItem.. " 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
-- Sell Item (Don't worry about this bit)
local NPCProxPromt = workspace.OminusOfficesMap.SellNPC["Hobo Henry"].Torso.ProximityPrompt