I found this code for a placement system and I would like someone to break it down and explain it to me. Copying this code and adding -- tags to explain each section would be really useful. Thanks in advance.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local moves = false
local i = .5

for _,buttons in pairs(script.Parent:GetChildren()) do
	if buttons:IsA("TextButton") then
		buttons.MouseButton1Click:connect(function()
			moves = true
			if mouse.TargetFilter ~= nil then
				return
			end
			local typeOf = game.ReplicatedStorage:FindFirstChild(buttons.Name)
			if not typeOf then
				return
			end
			print'test1'
			if script.Parent.Money.Gross.Value >= typeOf.Cost.Value then
				script.Parent.Money.Gross.Value = script.Parent.Money.Profit.Value - typeOf.Cost.Value
			buttons.Text = 'Left Click to Place'
			local building = typeOf:Clone()
			building.Parent = workspace
			mouse.TargetFilter = building
			building:MakeJoints()
			local money = building.Cost
			
			script.Parent.Money.Gross.Value = script.Parent.Money.Gross.Value+money.Value
			script.Parent.Money.Profit.Value = script.Parent.Money.Profit.Value+(money.Value*2)
			print'test2'
			
			local move = mouse.Move:connect(function()
				local base = workspace:FindFirstChild(player.Name..'base')
				if mouse.Target.Name ~= base.Name then
					error('error')
					return
				end
				if moves == true then
				local x = math.floor(mouse.Hit.x)+1
				local y = math.floor(mouse.Hit.y)+.5
				local z = math.floor(mouse.Hit.z)+1
				building:SetPrimaryPartCFrame(CFrame.new(x,y,z))
				building:SetPrimaryPartCFrame(CFrame.new(x,y,z)*CFrame.Angles(0,math.pi*i,0))
				local key, gpe = game:getService('UserInputService').InputBegan:wait()
				if not gpe then
					if key.KeyCode == Enum.KeyCode.R then
						building:SetPrimaryPartCFrame(CFrame.new(x,y,z)*CFrame.Angles(0,math.pi*i,0))
						i = i +.5
						if i==2.5 then
							i = .5
						end
					end
				end
				print 'test3'
				end
			end)
			end
		end)
		
			mouse.Button1Down:connect(function()
				if mouse.Target == nil then
					return
				end
				if moves == true then
			buttons.Text = buttons.Name
			mouse.TargetFilter = nil
			script.Disabled = true
			wait()
			script.Disabled = false
			moves = false
				elseif not moves then
					return
				end
				print 'test4'
			end)
	end
	end

1 comment

1

https://www.youtube.com/watch?v=HW8K6FblwvI --slightly similar to this, might help