How to identify the Item Name without If statements

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

2 Likes

Why do you need all the if statements? I don’t think you need the value of selectedItem, since it doesn’t change anything.

local function onProximityPromptTriggered()
	local selectedItem, selectedCost = getRandomItemInfo()
	
	local tool = Instance.new("Tool")
	tool.Name = selectedItem -- you don't need to know the value of selectedItem, unless there is something else you need to include in the if statement
	local handle = Instance.new("Part")
	handle.Name = "Handle"
	handle.Size = Vector3.new(1, 1, 1)
	handle.Parent = tool
	tool.Parent = player.Backpack
end
1 Like

Well what I want to do is to identify the Item and then it copies the build/asset (like so the newspaper doesn’t look like a 1x1 lego brick and actually looks like a newspaper) from replicated storage and puts the parent as the player’s backpack. Also the assets in replicated storage will be named accordingly to the names of the items. Also I haven’t made the assets yet…:cold_face:

Is there anyways to do this?

I did it by using 1 if statement and using the “selectedItem” that you said.

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

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.