Script Simplification

Hello developers, I need help with learning how to simplify scripts for example, the script following is really long, and there must be more efficient ways to do things.

local main = script.Parent.Main
local home = script.Parent.Home
local food = script.Parent.Food
local setting = script.Parent.Settings
local jokes = script.Parent.Jokes
local games = script.Parent.Games
local cam = script.Parent.Cameras
local shop = script.Parent.Shop

local home2 = home.Screen.LeftMenuBar.Frame
local food2 = home2.Food
local setting2 = home2.Settings
local jokes2 = home2.Jokes
local games2 = home2.Games
local shop2 = home2.Shop
local cam2 = home2.Cameras
local off2 = home.Screen.LeftMenuBar.Off

local player = game:GetService("Players").LocalPlayer

main.Welcome_Screen.Welcome.Text = "Welcome aboard, "..player.Name.."!"

main.Welcome_Screen.TextButton.MouseButton1Click:Connect(function()
	main:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	home:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)

off2.MouseButton1Click:Connect(function()
	home:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
end)

--Home Frame Area
food2.MouseButton1Click:Connect(function()
	--food2.Text = "Unfortunately this feature is unavailable at this time"
--	wait(5)
--  food2.Text = "Food Ordering"	
	home:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	food:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)

setting2.MouseButton1Click:Connect(function()
	home:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	setting:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)

jokes2.MouseButton1Click:Connect(function()
	home:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	jokes:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)

games2.MouseButton1Click:Connect(function()
	home:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	games:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)

shop2.MouseButton1Click:Connect(function()
	home:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	shop:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)

-- Game padge

games.Screen.romaze.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.IfeTools["RoMaze"]:Clone().Parent = player.Backpack
end)

games.Screen.lunarlander.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.IfeTools["LunarLander"]:Clone().Parent = player.Backpack
end)

games.Screen.TopBar.X.MouseButton1Click:Connect(function()
	games:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	home:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)

-- Food Padge

food.Screen.TopBar.X.MouseButton1Click:Connect(function()
	food:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	home:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
	wait(3)

end)

	--Mains
food.Screen.Mains.Main1.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Cheeseburger."
	food.Screen.YourOrder.Main.Order.Text = "Cheeseburger"
end)

food.Screen.Mains.Main2.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Chicken."
	food.Screen.YourOrder.Main.Order.Text = "Chicken"
end)

food.Screen.Mains.Main3.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Japanese Sushi."
	food.Screen.YourOrder.Main.Order.Text = "Japanese Sushi"
end)

food.Screen.Mains.Main4.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Plain Sandwich."
	food.Screen.YourOrder.Main.Order.Text = "Plain Sandwich"
end)

food.Screen.Mains.Main5.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Pumpkin Soup."
	food.Screen.YourOrder.Main.Order.Text = "Pumpkin Soup"
end)

food.Screen.Mains.Main6.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Salad."
	food.Screen.YourOrder.Main.Order.Text = "Salad"
end)

food.Screen.Mains.Main7.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Sandwich and cookie."
	food.Screen.YourOrder.Main.Order.Text = "Sandwich and cookie"
end)

food.Screen.Mains.Main8.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Steak."
	food.Screen.YourOrder.Main.Order.Text = "Steak"
end)

food.Screen.Mains.Main9.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Tomato Soup."
	food.Screen.YourOrder.Main.Order.Text = "Tomato Soup"
end)

food.Screen.Mains.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.Visible = false
	food.Screen.Drinks.Visible = true
end)

	-- Drinks
food.Screen.Drinks.Drinks1.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Apple Juice."
	food.Screen.YourOrder.Drink.Order.Text = "Apple Juice"
end)

food.Screen.Drinks.Drinks2.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Blueberry Juice."
	food.Screen.YourOrder.Drink.Order.Text = "Blueberry Juice"
end)

food.Screen.Drinks.Drinks3.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Caribbean Style Lemonade."
	food.Screen.YourOrder.Drink.Order.Text = "Caribbean Style Lemonade"
end)

food.Screen.Drinks.Drinks4.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Champane."
	food.Screen.YourOrder.Drink.Order.Text = "Champane"
end)

food.Screen.Drinks.Drinks5.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the CocaCola."
	food.Screen.YourOrder.Drink.Order.Text = "CocaCola"
end)

food.Screen.Drinks.Drinks6.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Fanta."
	food.Screen.YourOrder.Drink.Order.Text = "Fanta"
end)

food.Screen.Drinks.Drinks7.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Fruit Punch."
	food.Screen.YourOrder.Drink.Order.Text = "Fruit Punch"
end)

food.Screen.Drinks.Drinks8.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Orange Juice."
	food.Screen.YourOrder.Drink.Order.Text = "Orange Juice"
end)

food.Screen.Drinks.Drinks9.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Peach Mango Smoothie."
	food.Screen.YourOrder.Drink.Order.Text = "Peach Mango Smoothie"
end)

food.Screen.Drinks.Drinks10.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Pineapple Juice."
	food.Screen.YourOrder.Drink.Order.Text = "Pineapple Juice"
end)

food.Screen.Drinks.Drinks11.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Red Wine."
	food.Screen.YourOrder.Drink.Order.Text = "Red Wine"
end)

food.Screen.Drinks.Drinks12.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Sprite."
	food.Screen.YourOrder.Drink.Order.Text = "Sprite"
end)

food.Screen.Drinks.Drinks13.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the Water."
	food.Screen.YourOrder.Drink.Order.Text = "Water"
end)

food.Screen.Drinks.Drinks14.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.FS.Text = "You selected the White Wine."
	food.Screen.YourOrder.Drink.Order.Text = "White Wine"
end)

food.Screen.Drinks.X.MouseButton1Click:Connect(function()
	food.Screen.Drinks.Visible = false
	food.Screen.Deserts.Visible = true
end)
	--Deserts
food.Screen.Deserts.Deserts1.X.MouseButton1Click:Connect(function()
	food.Screen.Deserts.FS.Text = "You selected the Egg and cheesecake."
	food.Screen.YourOrder.Desert.Order.Text = "Egg and cheesecake"
end)

food.Screen.Deserts.Deserts2.X.MouseButton1Click:Connect(function()
	food.Screen.Deserts.FS.Text = "You selected the Cake."
	food.Screen.YourOrder.Desert.Order.Text = "Cake"
end)

--Submitt
food.Screen.Deserts.X.MouseButton1Click:Connect(function()
	food.Screen.Deserts.Visible = false
	food.Screen.YourOrder.Visible = true
end)


food.Screen.YourOrder.Submit.MouseButton1Click:Connect(function()
	if 	food.Screen.YourOrder.Main.Order.Text == "Cheeseburger" then
		game.ReplicatedStorage.IfeTools.Mains.Cheeseburger:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Main.Order.Text == "Chicken" then
		game.ReplicatedStorage.IfeTools.Mains.Chicken:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Main.Order.Text == "Japanese Sushi" then
		game.ReplicatedStorage.IfeTools.Mains["Japanese Sushi"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Main.Order.Text == "Plain Sandwich" then
		game.ReplicatedStorage.IfeTools.Mains["Plain Sandwich"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Main.Order.Text == "Pumpkin Soup" then
		game.ReplicatedStorage.IfeTools.Mains["Pumpkin Soup"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Main.Order.Text == "Salad" then
		game.ReplicatedStorage.IfeTools.Mains.Salad:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Main.Order.Text == "Sandwich and cookie" then
		game.ReplicatedStorage.IfeTools.Mains["Sandwich and cookie"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Main.Order.Text == "Steak" then
		game.ReplicatedStorage.IfeTools.Mains.Steak:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Main.Order.Text == "Tomato Soup" then
		game.ReplicatedStorage.IfeTools.Mains["Tomato Soup"]:Clone().Parent = player.Backpack	
	end
	if food.Screen.YourOrder.Drink.Order.Text == "Apple Juice" then
		game.ReplicatedStorage.IfeTools.Drinks["Apple Juice"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Blueberry Juice" then
		game.ReplicatedStorage.IfeTools.Drinks["Blueberry Juice"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Caribbean Style Lemonade" then
		game.ReplicatedStorage.IfeTools.Drinks["Caribbean Style Lemonade"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Champane" then
		game.ReplicatedStorage.IfeTools.Drinks["Champane"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "CocaCola" then
		game.ReplicatedStorage.IfeTools.Drinks["CocaCola"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Fanta" then
		game.ReplicatedStorage.IfeTools.Drinks["Fanta"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Fruit Punch" then
		game.ReplicatedStorage.IfeTools.Drinks["Fruit Punch"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Orange Juice" then
		game.ReplicatedStorage.IfeTools.Drinks["Orange Juice"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Peach Mango Smoothie" then
		game.ReplicatedStorage.IfeTools.Drinks["Peach Mango Smoothie"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Pineapple Juice" then
		game.ReplicatedStorage.IfeTools.Drinks["Pineapple Juice"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Red Wine" then
		game.ReplicatedStorage.IfeTools.Drinks["Red Wine"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Sprite" then
		game.ReplicatedStorage.IfeTools.Drinks["Sprite"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "Water" then
		game.ReplicatedStorage.IfeTools.Drinks["Water"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Drink.Order.Text == "White Wine" then
		game.ReplicatedStorage.IfeTools.Drinks["White Wine"]:Clone().Parent = player.Backpack

	end
	if food.Screen.YourOrder.Desert.Order.Text == "Egg and cheesecake" then
		game.ReplicatedStorage.IfeTools.Desserts["Egg and cheesecake"]:Clone().Parent = player.Backpack
	elseif food.Screen.YourOrder.Desert.Order.Text == "Cake" then
		game.ReplicatedStorage.IfeTools.Desserts["Cake"]:Clone().Parent = player.Backpack
	end
	food.Screen.YourOrder.Submit.Visible = false
	food.Screen.Frame.Visible = true
end)
-- Joke Padge 

jokes.Screen.TopBar.X.MouseButton1Click:Connect(function()
	jokes:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	home:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)

jokes.Screen.Joke1.X.MouseButton1Click:Connect(function()
	print("Joke 1 clicked")
	jokes.Screen.Joke1.Visible = false
	jokes.Screen.Joke2.Visible = true
end)

jokes.Screen.Joke2.X.MouseButton1Click:Connect(function()
	jokes.Screen.Joke2.Visible = false
	jokes.Screen.Joke3.Visible = true
end)

jokes.Screen.Joke3.X.MouseButton1Click:Connect(function()
	jokes.Screen.Joke3.Visible = false
	jokes.Screen.Joke4.Visible = true
end)

jokes.Screen.Joke4.X.MouseButton1Click:Connect(function()
	jokes.Screen.Joke4.Visible = false
	jokes.Screen.Joke5.Visible = true
end)

jokes.Screen.Joke5.X.MouseButton1Click:Connect(function()
	jokes.Screen.Joke5.Visible = false
	jokes.Screen.Joke6.Visible = true
end)

jokes.Screen.Joke6.X.MouseButton1Click:Connect(function()
	jokes.Screen.Joke6.Visible = false
	jokes.Screen.Joke7.Visible = true
end)

jokes.Screen.Joke7.X.MouseButton1Click:Connect(function()
	jokes.Screen.Joke7.Visible = false
	jokes.Screen.Joke8.Visible = true
end)

jokes.Screen.Joke8.X.MouseButton1Click:Connect(function()
	jokes.Screen.Joke8.Visible = false
	jokes.Screen.Joke9.Visible = true
end)

jokes.Screen.Joke9.X.MouseButton1Click:Connect(function()
	jokes.Screen.Joke9.Visible = false
	jokes.Screen.Joke1.Visible = true
end)



-- Settings Padge

setting.Screen.TopBar.X.MouseButton1Click:Connect(function()
	setting:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	home:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)

--[[setting.DarkMode.TextButton.MouseButton1Click:Connect(function()
	if setting.DarkMode.Value.Value == true then
		setting.Screen.BackgroundColor3 = Color3.fromRGB(125,125,125)
		setting.DarkMode.Value.Value = false
	else
		setting.Screen.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
		setting.DarkMode.Value.Value = true
	end
end) ]]

-- Shopping padge
shop.Screen.TopBar.X.MouseButton1Click:Connect(function()
	print("Shop close button pressed")
	shop:TweenPosition(UDim2.new(-2.308, 0,0.208, 0))
	wait(0.75)
	home:TweenPosition(UDim2.new(0.265, 0,0.208, 0),"Out","Back")
end)
shop.Screen.Main.Frame["Amenity Kit"].MouseButton1Click:Connect(function()
	game.ReplicatedStorage.IfeTools["Amenities Kit"]:Clone().Parent = player.Backpack	
end)

shop.Screen.Main.Frame["Teddy Bear"].MouseButton1Click:Connect(function()
	game.ReplicatedStorage.IfeTools["Teddy"]:Clone().Parent = player.Backpack	
end)

Great intuition! Did you know you can write functions to reuse code?

For example, consider this:

food.Screen.Mains.Main1.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Cheeseburger."
	food.Screen.YourOrder.Main.Order.Text = "Cheeseburger"
end)

food.Screen.Mains.Main2.X.MouseButton1Click:Connect(function()
	food.Screen.Mains.FS.Text = "You selected the Chicken."
	food.Screen.YourOrder.Main.Order.Text = "Chicken"
end)

-- ... more

You can rewrite this like this:

local function setup(gui, itemName)
	gui.MouseButton1Click:Connect(function()
		food.Screen.Mains.FS.Text = "You selected the Cheeseburger."
		food.Screen.YourOrder.Main.Order.Text = itemName
	end)
end

setup(food.Screen.Mains.Main1.X, "Cheeseburger")
setup(food.Screen.Mains.Main2.X, "Chicken")
setup(food.Screen.Mains.Main3.X, "Japanese Sushi")
-- more...

Think of “Chicken” being substituted into this function as an argument itemName!

Now you’ll see that we still have a leftover "You selected the Cheeseburger.". You’ll need to combine strings like this:

local function setup(gui, itemName)
	gui.MouseButton1Click:Connect(function()
		food.Screen.Mains.FS.Text = "You selected the " .. itemName .. "."
		food.Screen.YourOrder.Main.Order.Text = itemName
	end)
end

setup(food.Screen.Mains.Main1.X, "Cheeseburger")
setup(food.Screen.Mains.Main2.X, "Chicken")
setup(food.Screen.Mains.Main3.X, "Japanese Sushi")

See how we use string concatenation here? That’s the “..” which will combine strings. (The text inside of quotes (e.x. "hi"). This will save you a lot of repeated code!

2 Likes

Thank you so so very much, but will this work in a local script?

1 Like

it works in both local scripts and server scripts if you are not using UI’s server side because that’s only for the player. For example if you have code that changes the colour of a part and u don’t want 50 different lines that just change the colour, u can make one function and then send that function the colour you want the part to be + extra stuff like transparency, the name etc.

In my opinion this is a simple script. As long as you don’t do

local h = 03-458943
local gh = 30498580

Then i’d consider simple.

Allright, thank you all! :slight_smile: You have all been a massive help!

Regarding this section of code:

Since the order text corresponds to the item name, you can use the text to directly search for the item.

Consider the following substitute:

local yourOrder = game.ReplicatedStorage.IfeTools.Mains:FindFirstChild(food.Screen.YourOrder.Main.Order.Text) 
if yourOrder then --make sure there is something to clone
   local yourFood = yourOrder:Clone()
   yourFood.Parent = player.Backpack
end

No run-on if statements!

But there’s an even better way! You don’t need to store/search for text at all. If Order was an objectValue pointing to the food instance, you wouldn’t even have to look for it. Obviously I cant see the whole setup, but the code would look something like this.

local yourFood = food.Screen.YourOrder.Main.Order.Value:Clone()
--you already know it exists
yourFood.Parent = player.Backpack

This requires possible rethinking of your instances and folders, but leaves you with the most straightforward code.

Thank you very much, that will make it much easier to find an issue in the future! :slight_smile: