Basic Cafe Handler - How is it?

I personally believe this might have taken me too long and too many lines of code in order to make a functioning coffee and tea handler, with different flavors and what not.

So, how is it? How could I make this more efficient, or even shorter to script?

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local cookingObjectFolder = workspace.CookingObjects

local blenderFolder = cookingObjectFolder:WaitForChild('Blender')
local teaFlavorFolder = cookingObjectFolder:WaitForChild('TeaFlavor')
local coffeeFolder = cookingObjectFolder:WaitForChild('Coffee')
local waterFolder = cookingObjectFolder:WaitForChild('Water')
local milkFolder = cookingObjectFolder:WaitForChild('Milk')
local cupFolder = cookingObjectFolder:WaitForChild('Cup')
local iceFolder = cookingObjectFolder:WaitForChild('Ice')
local whippedCreamFolder = cookingObjectFolder:WaitForChild('WhippedCream')

local itemFolder = ReplicatedStorage:WaitForChild('Items')

local debounce = false
local hb = 1
local machineTime = 3

wait(machineTime)

local function visualEffects(tool, hitbox)
	tool.Visual1.Color = hitbox.Color
	tool.Visual1.Transparency = 0
end

local function coffeeMachineVisualizer(v, hitPart)
	v.Parent.Liquid.Transparency = 0

	for _, k in pairs(v.Parent.CupVisual:GetChildren()) do
		k.Transparency = 0.6

	end
	local char = hitPart.Parent.Parent
	local plr = Players:GetPlayerFromCharacter(char)
	hitPart.Parent.Parent = game.Lighting
	wait(machineTime)
	hitPart.Parent.Parent = plr.Backpack
	for _, k in pairs(v.Parent.CupVisual:GetChildren()) do
		k.Transparency = 1
	end

	v.Parent.Liquid.Transparency = 1
end

for _, v in pairs(cupFolder:GetDescendants()) do
	if v:IsA('ProximityPrompt') then
		v.Triggered:Connect(function(player)
			if not player.Character:FindFirstChild('Cup') and not player.Backpack:FindFirstChild('Cup') then
				local cupClone = itemFolder.Cup:Clone()
				cupClone.Parent = player.Backpack
			end
		end)
	end
end

for _, v in pairs(iceFolder:GetDescendants()) do
	if v.Name == "Hitbox" then
		local iceDebounce = false
		v.Touched:Connect(function(hitPart)
			if not iceDebounce and hitPart.Parent:IsA('Tool') then
				local nameCheck = string.sub(hitPart.Parent.Name, 0, 4)
				if nameCheck ~= "Iced" then
					hitPart.Parent.Name = "Iced "..hitPart.Parent.Name
					for _, ice in pairs(hitPart.Parent.Cup:GetChildren()) do
						if ice:IsA("Part") then
							ice.Transparency = 0
						end
					end
				end
				RunService.Heartbeat:Wait(hb)
				iceDebounce = true
			end
		end)
	end
end

for _, v in pairs(coffeeFolder:GetDescendants()) do
	if v.Name == "Hitbox" then
		local coffeDebounce = false
		v.Touched:Connect(function(hitPart)
			if not debounce and hitPart.Parent:IsA('Tool') then
				coffeDebounce = true
				if hitPart.Parent.Name == "Cup" then
					hitPart.Parent.Name = v.Parent.Name..' Coffee'
					coffeeMachineVisualizer(v, hitPart)
					visualEffects(hitPart.Parent, v)
				elseif hitPart.Parent.Name == "Hot Milk" then
					if v.Parent.Name == "Espresso" then
						hitPart.Parent.Name = "Cappuccino"
					end
					
					if v.Parent.Name == "Regular" then
						hitPart.Parent.Name = "Frappuccino"
					end
					
					coffeeMachineVisualizer(v, hitPart)
					visualEffects(hitPart.Parent, v)
				end
				RunService.Heartbeat:Wait(hb)
				coffeDebounce = false
			end
		end)
	end
end

for _, v in pairs(teaFlavorFolder:GetDescendants()) do
	if v.Name == "Hitbox" then
		local teaDebounce = false
		v.Touched:Connect(function(hitPart)
			if not debounce and hitPart.Parent:IsA('Tool') then
				if hitPart.Parent.Name == "Hot Water" then
					teaDebounce = true
					hitPart.Parent.Name = v.Parent.Name.." Tea"
					visualEffects(hitPart.Parent, v)
					RunService.Heartbeat:Wait(hb)
					teaDebounce = false
				end
			end
		end)
	end
end

for _, v in pairs(waterFolder:GetDescendants()) do
	if v.Name == "Hitbox" then
		local waterDebounce = false
		v.Touched:Connect(function(hitPart)
			if not waterDebounce and hitPart.Parent:IsA('Tool') then
				waterDebounce = true
				if hitPart.Parent.Name == "Cup" then
					if v.Parent.Name == "HotWater" then
						hitPart.Parent.Name = "Hot Water"
					elseif v.Parent.Name == "Water" then
						hitPart.Parent.Name = "Water"
					end
					visualEffects(hitPart.Parent, v)
				end
				RunService.Heartbeat:Wait(hb)
				waterDebounce = false
			end
		end)
	end
end

for _, v in pairs(milkFolder:GetDescendants()) do
	if v.Name == "Hitbox" then
		local milkDebounce = false
		v.Touched:Connect(function(hitPart)
			if not milkDebounce and hitPart.Parent:IsA('Tool') then
				milkDebounce = true
				if hitPart.Parent.Name == "Cup" then
					if v.Parent.Name == "HotMilk" then
						hitPart.Parent.Name = "Hot Milk"
					elseif v.Parent.Name == "Milk" then
						hitPart.Parent.Name = "Milk"
					end
					visualEffects(hitPart.Parent, v)
				end
				RunService.Heartbeat:Wait(hb)
				milkDebounce = false
			end
		end)
	end
end

for _, v in pairs(whippedCreamFolder:GetDescendants()) do
	if v.Name == "Hitbox" then
		local whippedCreamedDebounce = false
		v.Touched:Connect(function(hitPart)
			if not whippedCreamedDebounce and hitPart.Parent:IsA('Tool') then
				whippedCreamedDebounce = true
				hitPart.Parent.WhippedCream.Transparency = 0
				hitPart.Parent.Cover:Destroy()
				hitPart.Parent.Name = hitPart.Parent.Name.." w/ Cream"
				RunService.Heartbeat:Wait(hb)
				whippedCreamedDebounce = false
			end
		end)
	end
end
1 Like

Hi there! Your code looks clean, apart from a few spaces here and there, it could be more efficient. First of all at the variables, you have a separate variable for every machine.

local blenderFolder = cookingObjectFolder:WaitForChild(‘Blender’)
local teaFlavorFolder = cookingObjectFolder:WaitForChild(‘TeaFlavor’)
local coffeeFolder = cookingObjectFolder:WaitForChild(‘Coffee’)
local waterFolder = cookingObjectFolder:WaitForChild(‘Water’)
local milkFolder = cookingObjectFolder:WaitForChild(‘Milk’)
local cupFolder = cookingObjectFolder:WaitForChild(‘Cup’)
local iceFolder = cookingObjectFolder:WaitForChild(‘Ice’)
local whippedCreamFolder = cookingObjectFolder:WaitForChild(‘WhippedCream’)

This can be done more efficiently. By having an automated system that has all of the objects in one folder and making it so every cup in the folder would be automatic. If this isn’t possible I suggest doing it into a table.

I am confused… Those variables are assigned to the folder in which the machines of that type are in.

I probably misunderstood your point…

1 Like

I din’t understand what he said the best, but I guess he said he wants you to make an folder with everything, and just make an table with everything

Example:

local Machines = cookingObjectFolder:GetChildren()
local CurrentMachines = {}
for _,Machine in ipairs(Machines) do
      CurrentMachines[Machine.Name] = Machine
end
Machines = CurrentMachines

And you can index from it saying

Machines.whippedCreamFolder:Destroy() -- or something to do 

Note that the code isn’t the best, because it is 1AM.

2 Likes

Is there really any difference between me making multiple variables for each folder rather than make it in a table?

1 Like

Maybe just saving up some speed and lines, but nothing more.

1 Like