I’m having difficulty making if statements fire after renaming a tool and I’m unsure why this is the case or if there is a way to fix it.
I’m 100% sure that the if statement is true for the lines I’m having issues with.
elseif machine.Name == "LatteMachine" then
print(cup.Name)
if hit.Parent.Name == "Espresso" then
cup.Name = "Latte"
print(cup.Name)
These are the two if statements that I can’t get to work.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local directory = workspace.CafeMachines
local AddOns = directory.AddOns:GetDescendants()
local Coffees = directory.Coffees:GetDescendants()
local IceCreams = directory.IceCreams:GetDescendants()
local Teas = directory.Teas:GetDescendants()
local Cups = directory.Cups:GetDescendants()
local cups = {
"Large Cone";
"Medium Cone";
"Small Cone";
"Large Cup";
"Medium Cup";
"Small Cup";
"Large Glass";
"Medium Glass";
"Small Glass";
"Large Mug";
"Medium Mug";
"Small Mug";
}
for _,dir in pairs(directory:GetDescendants()) do
if dir:IsA("Part") and dir.Name == "Dispenser" then
dir.Touched:Connect(function(hit)
for _,valid in pairs(cups) do
if hit.Parent.Name == valid then
local cup = hit.Parent
local machine = dir.Parent
cup.Changed:Connect(function()
print("Cup name changed to "..cup.Name..".") return
end)
--COFFEE
if machine.Name == "RegularMachine" then
cup.Name = "Regular"
elseif machine.Name == "DecafMachine" then
cup.Name = "Decaf"
elseif machine.Name == "EspressoMachine" then
cup.Name = "Espresso"
cup.Flavour.Value = "Espresso"
elseif machine.Name == "LatteMachine" then
print(cup.Name)
if hit.Parent.Name == "Espresso" then
cup.Name = "Latte"
print(cup.Name)
elseif machine.Name == "CappuccinoMachine" then
cup.Name = "Cappuccino"
end
end
end
end
end)
end
end
elseif machine.Name == "LatteMachine" then
print(cup.Name)
if hit.Parent.Name == "Espresso" then
cup.Name = "Latte"
print(cup.Name)
elseif machine.Name == "CappuccinoMachine" then
My question is why you’re having the same script over multiple machines? It would be MUCH better to make each machine it’s own handler. Would help with debugging the issue as well.
If the issues lies within all of the IF/ELSES inside of eachother, and using a dictionary would remove the need for any IF/ELSES, I’d think that this could definitely solve the issue. If anything, it would make your code more readable for you to more easily figure out where the issue is.
I’ve even tried using StringValues to store the changed properties of the cup, and the if statements failed to fire on that, so I can assure you that using an array wouldn’t make a difference, but I’ll give it a shot.
I would first remove the cup changed event as this might give you invalid results.
In terms of your code I would also break down the code by including additional functions and follow what @Ancientroboman said about using tables. To prevent spam I would also include a debounce.
For debugging put a brekpoint into the touched function and step through the code.
You should be able to do something like this…
local validCupNames = {
["Large Cone"] = true,
...
}
local machineNames = {
["RegularMachine"] = "Regular",
...
}
for _,dir in pairs(directory:GetDescendants()) do
if dir:IsA("Part") and dir.Name == "Dispenser" and machineNames[dir.Parent.Name] then
dir.Touched:Connect(function(hit)
-- inc debounce
if not validCupNames[hit.Parent.Name] then return end -- avoid nesting loops
hit.Parent.Name = machineNames[dir.Parent.Name]
end)
end
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local directory = workspace.CafeMachines
local AddOns = directory.AddOns:GetDescendants()
local Coffees = directory.Coffees:GetDescendants()
local IceCreams = directory.IceCreams:GetDescendants()
local Teas = directory.Teas:GetDescendants()
local Cups = directory.Cups:GetDescendants()
local cups = {
"Large Cone";
"Medium Cone";
"Small Cone";
"Large Cup";
"Medium Cup";
"Small Cup";
"Large Glass";
"Medium Glass";
"Small Glass";
"Large Mug";
"Medium Mug";
"Small Mug";
}
for _,dir in pairs(directory:GetDescendants()) do
if dir:IsA("Part") and dir.Name == "Dispenser" then
dir.Touched:Connect(function(hit)
for _,valid in pairs(cups) do
if hit.Parent.Name == valid then
local cup = hit.Parent
local machine = dir.Parent
cup.Changed:Connect(function()
print("Cup name changed to "..cup.Name..".") return
end)
if machine.Name == "RegularMachine" then
cup.Name = "Regular"
elseif machine.Name == "DecafMachine" then
cup.Name = "Decaf"
elseif machine.Name == "EspressoMachine" then
cup.Name = "Espresso"
cup.Flavour.Value = "Espresso" --Why is this the only one with the 'cup.Flavour' value?
elseif machine.Name == "LatteMachine" then
print(cup.Name)
if hit.Parent.Name == "Espresso" then
cup.Name = "Latte"
print(cup.Name)
end --added an end for this statement
elseif machine.Name == "CappuccinoMachine" then
cup.Name = "Cappuccino"
--removed that one random end here
end
end
end
end)
end
end