How could i improve this code? and optimize it?

Hi, so i have been scripting my upcoming game, The underGround, I have been told alot that the code for this game isnt very efficient
image

local Interaction_System = function(Player)
	if Player then
		local ContextAction = game:GetService("ContextActionService")
		local RunService = game:GetService("RunService")
		local TweenService = game:GetService("TweenService")
		local UserInput = game:GetService("UserInputService")
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		local GuiService = game:GetService("GuiService")
		
		local Mouse = Player:GetMouse()
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local Humanoid = Character:WaitForChild("Humanoid")
		
		local TweenInfo1 = TweenInfo.new(1)
		local OldBillboard = nil
		
		local Client_Resources = require(ReplicatedStorage.Resources.Game_Resources:WaitForChild("Client_Resources"))
		
		local Pick_UP = Humanoid.Animator:LoadAnimation(script.Parent.Parent.Animations.Tool_PickUp)
			
		local CurrentDevice = function()
			if UserInput.TouchEnabled == true then
				return "Mobile"
			elseif UserInput.TouchEnabled == false then
				return "Computer"
			elseif UserInput.GamepadEnabled == true then
				return "Console"
			end
		end
		
		local Get_Current_INT = function()
			for _, INT in pairs(workspace.Game_.Resources_.Interactions:GetChildren()) do
				if INT then
					local Distance = Player:DistanceFromCharacter(INT.Position) 
					if Player:DistanceFromCharacter(INT.Position) <= 10 then
						if Player.Backpack:FindFirstChild(INT.Name) == nil and Player.Character:FindFirstChild(INT.Name) == nil then
							if Humanoid.Health > 0 then
								if Mouse.Target == INT then
									return INT, Mouse
								end
							end
						end
					end
				end
			end
		end
		local Main_Int = function(Name, Action)
			if Name == "Interact" then
				if Action == Enum.UserInputState.Begin then
					local CurrentInt, Mouse = Get_Current_INT()
					if CurrentInt then
						OldBillboard = CurrentInt
						ReplicatedStorage.Resources.Game_Resources.Client_Remote:FireServer(CurrentInt, Mouse.Target.Name, "INT")
						if Client_Resources.INT.Interation_Items[CurrentInt.Name].Destroy_PickUp == true then
							Pick_UP:Play()
						end
					end
				end
			end
		end
		ContextAction:BindAction("Interact", Main_Int, true, Enum.KeyCode.E, Enum.KeyCode.LeftBracket)
		local Int_Message = function()
			local CurrentInt = Get_Current_INT()
			if CurrentInt then
				CurrentInt.BillboardGui.Enabled = true
				local Device = CurrentDevice()
				TweenService:Create(Player.PlayerGui.Main_Resource.Interaction_Text, TweenInfo1, {TextTransparency = 0}):Play()
				if Device == "Computer" then
					Player.PlayerGui.Main_Resource.Interaction_Text.Text = Client_Resources.INT.Devices.Computer.. Client_Resources.INT.Interation_Items[CurrentInt.Name].Description
				elseif Device == "Mobile" then
					Player.PlayerGui.Main_Resource.Interaction_Text.Text = Client_Resources.INT.Devices.Mobile.. Client_Resources.INT.Interation_Items[CurrentInt.Name].Description
				elseif Device == "Console" then
					Player.PlayerGui.Main_Resource.Interaction_Text.Text = Client_Resources.INT.Devices.Console.. Client_Resources.INT.Interation_Items[CurrentInt.Name].Description
				end
			else
				TweenService:Create(Player.PlayerGui.Main_Resource.Interaction_Text, TweenInfo1, {TextTransparency = 1}):Play()
			end
		end
		RunService.Stepped:Connect(Int_Message)
	end
end
return Interaction_System

Interation
and the server

local Server_Module = function()
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	
	local Int_Table = require(ReplicatedStorage.Resources.Game_Resources.Client_Resources)
	local Client_Remotes = function()
		ReplicatedStorage.Resources.Game_Resources.Client_Remote.OnServerEvent:Connect(function(Player, Object, Mouse, Type)
			if Player and Mouse and Object and Type then
				local Current_Table = Int_Table.INT.Interation_Items[Object.Name]
				if Type == "INT" then
					if Mouse == Object.Name and Player:DistanceFromCharacter(Object.Position) <= 10 then
						if Current_Table.Type == "Pick_Up" then
							game.ServerStorage.Resources.Interactables[Object.Name]:Clone().Parent = Player.Backpack
						end
						if Current_Table.Destroy_PickUp == true then
							wait(0.5)
							Object:Destroy()
							Player.Character.Equip:Play()
						end
					end
				elseif Type == "TOOL" then
					if Current_Table.Action == "Flash" then
						if Current_Table.Activated  == false and Current_Table.Cooldown == false then
							Current_Table.Cooldown = true
							Object.LightPart.TurnOn:Play()
							Object.LightPart.LightPart.Enabled = true
							Current_Table.Activated  = true
							wait(1)
							Current_Table.Cooldown = false
						else if Current_Table.Activated  == true and Current_Table.Cooldown == false then
								Current_Table.Activated  = false
								Current_Table.Cooldown = true
								Object.LightPart.TurnOff:Play()
								Object.LightPart.LightPart.Enabled = false
								wait(1)
								Current_Table.Cooldown = false
							end
						end
					end
				end
			end
		end)
	end
	Client_Remotes()
end
return Server_Module

I didn’t inspect every line of the code, but it looks like you have a lot of nested fors and ifs.

The general good practice is that each function has 1 responsibility and it does it well. Wikipedia


may be instead of

do game.ReplicatedStorage

I dont think that really makes a difference ._.

using GetService is recommended, what they did is fine

though I would define those variables outside of the function instead

local ContextAction = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local UserInput = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GuiService = game:GetService("GuiService")

local Interaction_System = function(Player)
	if Player then

Make all the services on the client global variables.