Me and my friend have worked together on this placement system script for a while now as a school project.
I need to make this code shorter and more efficient/clean.
The whole code is here:
wait()
local rs = game:WaitForChild("ReplicatedStorage")
local base = game.Workspace.GrassTop
local placeablePart = rs:WaitForChild("PlaceableObjects")
local Menu = game.Players.LocalPlayer.PlayerGui.PlacementSystem.ObjectMenu.ObjectList
local CurrentlyPlacing = false
local rotation = 0
local canPlace = false
local first = true
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService("RunService")
function roundAToB(a,b)
return(math.floor((a/b)+0.5)*b)
end
local modelPreview, modelPlace
modelRotate = mouse.KeyDown:connect(function(key)
if (key == "r") then
rotation = rotation + 90
end
end)
---------- OBJECTS ----------
Menu.Wall.MouseButton1Click:connect(function()
script.SelectedPart.Value = placeablePart.Wall
CurrentlyPlacing = true
cloneModel = script.SelectedPart.Value:clone()
cloneModel.Parent = game.Workspace
end)
-----------------------------
count = 0
modelPreview = runService.RenderStepped:connect(function()
if CurrentlyPlacing == true then
local modelCFrame = cloneModel:GetModelCFrame()
mouse.TargetFilter = cloneModel
local hit = mouse.Hit.p
cloneModel:SetPrimaryPartCFrame(CFrame.new(roundAToB(hit.X,4),1.5,roundAToB(hit.Z,4)) * CFrame.Angles(0, math.rad(rotation), 0))
if (cloneModel.PrimaryPart.Position.X > base.Position.X-base.Size.X/2) and (cloneModel.PrimaryPart.Position.Z > base.Position.Z-base.Size.Z/2) and (cloneModel.PrimaryPart.Position.X < base.Position.X+base.Size.X/2) and (cloneModel.PrimaryPart.Position.Z < base.Position.Z+base.Size.Z/2) then
canPlace = true
cloneModel:Destroy()
cloneModel = script.SelectedPart.Value:clone()
cloneModel.Parent = game.Workspace
cloneModel:SetPrimaryPartCFrame(CFrame.new(roundAToB(hit.X,4),1.5,roundAToB(hit.Z,4)) * CFrame.Angles(0, math.rad(rotation), 0))
-- error is somewhere around here
for i,v in pairs(cloneModel:GetChildren()) do
if v:IsA('BasePart') then
v.Transparency = 0.7
v.CanCollide = false
end
end
else
canPlace = false
for i,v in pairs(cloneModel:GetChildren()) do
if v:IsA('BasePart') then
v.Transparency = 1
v.CanCollide = false
end
end
end
else
if not first then
cloneModel:Destroy()
end
end
end)
modelPlace = mouse.Button1Down:connect(function()
if CurrentlyPlacing == true then
if canPlace == true then
local new = cloneModel:clone()
new.Parent = workspace.Tycoons.TycoonTemplate.PurchasedObjects
for i,v in pairs(new:GetChildren()) do
if v:IsA('BasePart') then
v.Transparency = 0
v.CanCollide = true
-- Specific part properties below:
elseif v.Name == "Arrow" then
v.CanCollide = false
v.Transparency = 0.7
elseif v.Name == "Drop" then
v.CanCollide = false
v.Transparency = 0.5
end
end
end
cloneModel:Destroy()
script.CurrentlyPlacing.Value = false
canPlace = false
end
end)

2 comments
Is there anything actually broken in this code? I'm happy to look over it for you but it's gonna be on the backburner if there are no actual bugs to fix.
Since there is no indication that this code isn't functioning, i'm going to moderate. +1 to omit rep loss.