Problem with if statements

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

2 Likes
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

Should this not be elseif ?

1 Like

It wouldn’t make a difference, the first line doesn’t fire anyways.

1 Like

Why not replace this long list of nested IF/ELSE’s with a dictionary?
Do something like this:

local MyDictionary = {
   ["RegularMachine"] = "Regular",
   ["DecafMachine"] = "Decaf",
   ... --and so on
}

cup.Name = MyDictionary[machine.Name]

Dictionaries are like arrays, if you could name each spot of info inside of it. I think this would be a much better way to organize your code.

2 Likes

That doesn’t solve the problem.

1 Like

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.

That still wouldn’t solve the tool renaming problem.

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.

1 Like

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
1 Like

Thanks, I’ll use your advice and give it a shot.

I’ve just have re-read the whole script. One of the issues is that you forgot to add an “end” at this part;

if hit.Parent.Name == "Espresso" then
                            cup.Name = "Latte"
                            print(cup.Name)
                            end -- End goes here
                    elseif machine.Name == "CappuccinoMachine" then

Might be a nitpick, but still better than having a literally unreadable code.

Placing an end there results in an error.

There you go.

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

Alright thanks, I’ll give it a shot.

Thanks for the improvements to the script, but it gives me the exact same problem of the if statement not firing after the tool name has changed.

1 Like

Can you say what exactly you are trying to accomplish, again?

if not validCupNames[hit.Parent.Name] then return end -- avoid nesting loops

I’ve filled out the tables and this line won’t fire.

hit.Parent.Name = machineNames[dir.Parent.Name]

And this line done nothing.

A system to create coffees, teas and milkshakes with recipes.

I’d be happy to provide you with a .rbxm file.