Placement System Code Review

Hello, I made a placement system, and I wanna see how much more I can improve.

Client Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("Structure")
local Structures = game.ReplicatedStorage:WaitForChild("Walls")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local StructureFrame = script.Parent
local char = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

local Mouse = player:GetMouse()

local yBuildingOffset = 5
local maxPlacingDistance = 0
local rKeyIsPressed = false
local placingStructure = false



for _, structureButton in pairs(StructureFrame:GetChildren()) do
	if structureButton:IsA("TextButton") then
		structureButton.MouseButton1Up:Connect(function()
			if placingStructure == false then
				placingStructure = true

				local goodToplace
				
				local clientStructure = Structures:FindFirstChild(structureButton.Name):Clone()
			
				clientStructure.BrickColor = BrickColor.new("Lime green")
				clientStructure.Material = "Neon"
				
				clientStructure.CanCollide = false

				clientStructure.Parent = game.Workspace
	
				print(clientStructure.Name)


				RunService.RenderStepped:Connect(function()
					if placingStructure == true and clientStructure ~= nil then
						local mouseRay = Mouse.UnitRay
						local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
						local ignoreList = {char, clientStructure}						

						local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)

						clientStructure.Position = Vector3.new(position.X, position.Y + 5, position.Z)
						
						if hit and hit:IsA("Terrain") and (HumanoidRootPart.Position - clientStructure.Position).magnitude < 50 then
							goodToplace = true
							clientStructure.BrickColor = BrickColor.new("Lime green")
						else
							goodToplace = false
							clientStructure.BrickColor = BrickColor.new("Crimson")
						end


					end
				end) -- Run Service

			UIS.InputBegan:Connect(function(input, chat)
				if chat then return end
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					if goodToplace == true and placingStructure == true then
						print(clientStructure.Name.. " is good to place!")
						local Placed = PlaceStructure:InvokeServer(clientStructure.Name, clientStructure.CFrame)
						if Placed == true then
							clientStructure:Destroy()
							
							placingStructure = false
							print("Successfully placed and destroyed: "..clientStructure.Name)
						end
					else
						print(clientStructure.Name.. " is not good to place!")
					end
				end
			end)

			UIS.InputBegan:Connect(function(input, chat)
				if chat then return end
					if input.KeyCode == Enum.KeyCode.R then
					if placingStructure == true then
						rKeyIsPressed = true
						if rKeyIsPressed == true then
							clientStructure.CFrame = clientStructure.CFrame * CFrame.Angles(0, math.rad(45),0)
						end
					else
						clientStructure.CFrame = clientStructure.CFrame
					end
				end
			end)

			UIS.InputEnded:Connect(function(input, key)
				if key then return end
				if input.KeyCode == Enum.KeyCode.R then
					if rKeyIsPressed == true then
						rKeyIsPressed = false
					end
				end
			end)

				

			end
		end)
	end
end

Server Code:

local Walls = game.ReplicatedStorage:WaitForChild("Walls")

local Structure = game.ReplicatedStorage:WaitForChild("Structure")

Structure.OnServerInvoke = function(Player, StructureName, StructureCFrame)
	local crafted = false

	local realstructure = Walls:FindFirstChild(StructureName):Clone()

	if realstructure then
		print(realstructure.Name)
		realstructure.CFrame = StructureCFrame
		realstructure.Parent = game.Workspace
		crafted = true
	end

	return crafted

end

Provide an overview of:

  • What does the code do and what are you not satisfied with?
  • What potential improvements have you considered?
  • How (specifically) do you want to improve the code?

I think you should invest some of your time into researching modules. Think they can definitely organize this a lot better.

As Arxk said, modules, will help a lot. But, you also need to add the checks for the distance from the player server side as well, as someone could easily get around it being on the client only. :slight_smile:

I would improve readability if I were you. Add comments and organize your code. It’s pretty decent already, but you could go the extra mile. Add comments that describe what functions and event connections do.

Thanks for your feedback, I will do that.

1 Like