Rust Building System

So, I am trying to improve what was posted before https://devforum.roblox.com/t/placement-system-using-attachments-creates-multiple-walls/728563

Here is how I have my foundation setup

Here is the code I have modified slightly

local RS = game:GetService("ReplicatedStorage")


local ClickEvent = RS.Events.PlacementEvent
local Structures = RS:WaitForChild("Structures")
local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local isPlacing = false
local ToolEquiped = false
local Tool = script.Parent

local StructurePos

Tool.Equipped:Connect(function()
	ToolEquiped = true
	Main()


	local enabled = true
	Mouse.Button1Down:Connect(function()
		if not enabled then return end
		enabled = false
		ClickEvent:FireServer(StructurePos)
		wait(0.1)
		enabled = true
	end)
end)

function Main()
	while ToolEquiped == true do
		wait(0.1)

		local Foundation = Structures:FindFirstChild("Foundation"):Clone()
		Foundation.CanCollide = false
		Foundation.Parent = game.Workspace
		Foundation.Transparency = 0.25
		Foundation.BrickColor = BrickColor.new("Lime green")
		Foundation.Material = Enum.Material.ForceField
		
		local MouseTarget = Mouse.Target
		local target
		
		if MouseTarget then 
			target = MouseTarget.name
		end
		if target == "Foundation" then
			local ClosestDistance = math.huge
			local ClosestAttachment
			for _,attachment in pairs(Mouse.Target:GetChildren()) do
				if attachment.Name == "Foundation" then
					local mousePos = Mouse.Hit.p
					local worldPos = attachment.WorldPosition
					local distance = (mousePos - worldPos).Magnitude
					if distance < ClosestDistance then
						ClosestDistance = distance
						ClosestAttachment = attachment
					end
				
				end
			end
			RunService.RenderStepped:Connect(function()
				Mouse.TargetFilter = Foundation
				
				local objCFrame
				objCFrame = CFrame.new(ClosestAttachment.Position)
				objCFrame = objCFrame * CFrame.new(-Foundation.Foundation.Position)
				objCFrame = objCFrame * CFrame.Angles(0, math.rad(ClosestAttachment.Orientation.Y), 0)
				Foundation.CFrame = objCFrame
				StructurePos = Foundation.Position
				--objCFrame = CFrame.new(Mouse.Hit.Position.X, Mouse.Hit.Position.Y + Foundation.Size.Y / 2, Mouse.Hit.Position.Z)
				--Foundation.CFrame = CFrame.new(objCFrame.Position)
			end)
		end
		Tool.Unequipped:Connect(function()
			ToolEquiped = false
			Foundation:Destroy()
		end)
	end
end

Here is my issue I’m running into. I’ve tried doing a little more research and have slightly gotten lost at reading the WiKi’s

You are cloning it in the while loop

function Main()
	while ToolEquiped == true do
		wait(0.1)

		local Foundation = Structures:FindFirstChild("Foundation"):Clone()

Try this instead

function Main()
	if not ToolEquiped then return end
	local Foundation = Structures:FindFirstChild("Foundation"):Clone()
	while ToolEquiped == true do
		wait(0.1)
1 Like