I have a machine in a game, that when an item touches it, it gives a description of the item on a nearby sign. The main issue is that my game has 34 items, and more will likely be made before release. As of right now, the code is… messy:
collector.Touched:connect(function(hit)
if hit.Name == "Handle" and hit.Parent.Name == "Item" then
print("Item Entered")
sign.Title.Text = "Processing"
wait(5)
sign.Title.Text = hit.Parent.Name
sign["List of Combinations"].Text = "no combinations :("
hit.Position = spawnPart.Position
elseif hit.Name == "Handle" and hit.Parent.Name == "Item1" then
print("Item1 Entered")
sign.Title.Text = "Processing"
wait(5)
sign.Title.Text = hit.Parent.Name
sign["List of Combinations"].Text = "no combinations :("
hit.Position = spawnPart.Position
elseif hit.Name == "Handle" and hit.Parent.Name == "Item2" then
print("Item2 Entered")
sign.Title.Text = "Processing"
wait(5)
sign.Title.Text = hit.Parent.Name
sign["List of Combinations"].Text = "no combinations :("
hit.Position = spawnPart.Position
--continue x31
I know there’s a better way to do this. I’m just not sure what that is.
local itemCombinations = {
["Item2"] = "this item is useful",
["Item1"] = "blah blah blah"
}
collector.Touched:connect(function(hit)
if hit.Name == "Handle" then
local itemDescription = itemCombinations[hit.Parent.Name]
print("Item Entered")
sign.Title.Text = "Processing"
wait(5)
sign.Title.Text = hit.Parent.Name
sign["List of Combinations"].Text = itemDescription or "no combinations :(" --if it's nil then say no combinations
hit.Position = spawnPart.Position
end
end)
EDIT: You should also probably add a boolean so the machine can’t be used while it is processing
local itemCombinations = {
["Item2"] = "this item is useful",
["Item1"] = "blah blah blah"
}
local isProcessing = false --we use this value so the machine won't be used when it is processing
collector.Touched:connect(function(hit)
if hit.Name == "Handle" and not isProcessing then
local itemDescription = itemCombinations[hit.Parent.Name]
print("Item Entered")
isProcessing = true --machine is processing
sign.Title.Text = "Processing"
wait(5)
sign.Title.Text = hit.Parent.Name
sign["List of Combinations"].Text = itemDescription or "no combinations :(" --if it's nil then say no combinations
hit.Position = spawnPart.Position
isProcessing = false --done processing, other stuff can now process
end
end)
Only issue is that each item has it’s own description, so I’d have to either manually write them into the item somehow, or just use dictionaries like @mniao mentioned.
but you will still be writing them down each still. You can use an if statement to check if it has a description in the instance as well. Depends on which you prefer to be fair
The use of an if statement is not necessary as i already use or, as for the writing down thing, yes he will still have to do that but the entire code won’t have to be rewritten everytime like he previously did and which he really didn’t have to.