Help for Optimizing Script

I’m Making a Building system and I made it work but I think there’s a room for Optimization.

LocalScript inside Tool

--// Services //--
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local buildAttachment = {Floor = "FloorAttachment", Wall = "WallAttachment"}

--// Variable //--
local Player = Players.LocalPlayer
local Character = Player.Character
local GUI = Player.PlayerGui
local tool = script.Parent

local Mouse = Player:GetMouse()
--Values--
local GoodToPlace = false
local Attachment = nil

local Cloned 
local connection

--// Functions //--
local function guiCheck()
	local frame = GUI.Building.Main
	
	for	i, v in pairs(frame:GetChildren()) do
		if v:IsA("TextButton") then
			
			v.MouseButton1Click:Connect(function()
				if Cloned ~= nil then Cloned:Destroy() end
				Cloned = RS:FindFirstChild(v.Name):Clone()
				Cloned.Parent = game.Workspace
				print(Cloned)
			end)
			
		end
	end
end

local function UnitRay()
	local ur = Mouse.UnitRay
	return ur.Origin, ur.Direction * 10000
end
--// Main //--
tool.Equipped:Connect(function()
	if GUI.Building.Main.Visible == false then GUI.Building.Main.Visible = true end
	guiCheck()
	
	connection = RunService.RenderStepped:Connect(function()
		if Cloned ~= nil then
		local Origin, Direction = UnitRay()
		local Params = RaycastParams.new()

		Params.FilterType = Enum.RaycastFilterType.Blacklist
		Params.FilterDescendantsInstances = {Character, Cloned}

		local Raycast = workspace:Raycast(Origin, Direction, Params)

		if Raycast and Raycast.Instance then
			local raycastInstance = Raycast.Instance
			local nearestAttachment = nil

			for i, attachment in pairs(raycastInstance:GetChildren()) do
				if attachment:IsA("Attachment") then
					if attachment.Name == buildAttachment[Cloned.Name] then
						local attachmentPosition = attachment.WorldPosition
						local RaycastPosition = Raycast.Position

						if (RaycastPosition - attachmentPosition).Magnitude < 10 then
							if attachment:GetAttribute("Placed") == false then
								nearestAttachment = attachment
								Attachment = attachment

								local buildCframe = raycastInstance.CFrame * CFrame.new(attachment.Position)
								buildCframe = buildCframe * CFrame.new(-Cloned.FloorAttachment.Position)
								buildCframe = buildCframe * CFrame.Angles(0, math.rad(attachment.Orientation.Y), 0)
								Cloned.CFrame = buildCframe

								if GoodToPlace ~= nil then
									GoodToPlace = true
									Cloned.Color = Color3.fromRGB(0,255,0)
								end
							end
						end
					end
				end
				if nearestAttachment == nil  and GoodToPlace ~= nil then
					Cloned.CFrame = CFrame.new(Raycast.Position.X, Raycast.Position.Y + Cloned.Size.Y/2, Raycast.Position.Z)
					GoodToPlace = false
					Cloned.Color = Color3.fromRGB(255,0,0)
				end
			end
		end	
		end	
	end)
end)

tool.Unequipped:Connect(function()
	if GUI.Building.Main.Visible == true then GUI.Building.Main.Visible = false end
	if Cloned ~= nil then
		Cloned:Destroy()
		Cloned = nil
		connection:Disconnect()
	end
end)

What should I change? Any help will be appreciated.

1 Like

Update: I made a Mouse Module to make things easier. I removed repeat wait and just added a Floor Part to the Cloned Variable

1 Like