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