sure, here is the full script. The raycasts are on lin 189, so near end
local module = {}
local RR3 = require(game.ReplicatedStorage.RotatedRegion3)
module.place = function(plr,item)
local placeitem = game.ReplicatedStorage.Remotes.PlaceItem
local canPlace = false
local itemTransparent = item:Clone()
itemTransparent.Parent = workspace.Placing
local function refreshcolor()
if canPlace == true then
for i,v in pairs(itemTransparent.Parts:GetChildren()) do
if v.Name == "Hitbox" then
v.Transparency = 0.5
v.Color = Color3.fromRGB(0, 214, 0)
v.CanCollide = false
else
v.Transparency = 0.5
v.CanCollide = false
end
end
else
for i,v in pairs(itemTransparent.Parts:GetChildren()) do
if v.Name == "Hitbox" then
v.Transparency = 0.5
v.Color = Color3.fromRGB(200, 0, 0)
v.CanCollide = false
else
v.Transparency = 0.5
v.CanCollide = false
end
end
end
end
refreshcolor()
local blacklist = {}
local plot
for i,v in pairs(game.Workspace.Plots:GetChildren()) do
if v.Owner.Value == plr.Name then
plot = v
for i,b in pairs(v.BaseParts:GetChildren()) do
b.Grid.Transparency = 0
end
end
end
for i,v in pairs(itemTransparent.Parts:GetChildren()) do
table.insert(blacklist,v)
end
for i,v in pairs(plot.BuildAreas:GetChildren()) do
table.insert(blacklist,v)
end
local mouse = plr:GetMouse()
local hitbox = itemTransparent.Parts.PrimaryPart
local function snapToGrid(vector3,middle,folder,posx,posz,normal,hit,S1,S2,S3)
local x = math.floor(vector3.X+0.5)
local y = vector3.Y
local z = math.floor(vector3.Z+0.5)
wait()
local nx,ny,nz = normal.X,normal.Y,normal.Z
if nx ~= 0 and (ny == 0 and nz == 0) then
if nx > 0 then
print("Front")
if S1 == "Right" or S1 == "Left" then
x -= hitbox.Size.X/2
elseif S1 == "Top" or S1 == "Bottom" then
x -= hitbox.Size.Y/2
elseif S1 == "Front" or S1 == "Back" then
x -= hitbox.Size.Z/2
end
else
print("Back")
if S1 == "Right" or S1 == "Left" then
x += hitbox.Size.X/2
elseif S1 == "Top" or S1 == "Bottom" then
x += hitbox.Size.Y/2
elseif S1 == "Front" or S1 == "Back" then
x += hitbox.Size.Z/2
end
end
elseif ny ~= 0 and (nx == 0 and nz == 0) then
if ny > 0 then
print("Top")
if S2 == "Right" or S2 == "Left" then
y += hitbox.Size.X/2
elseif S2 == "Top" or S2 == "Bottom" then
y += hitbox.Size.Y/2
elseif S2 == "Front" or S2 == "Back" then
y += hitbox.Size.Z/2
end
else
print("Bottom")
if S2 == "Right" or S2 == "Left" then
y -= hitbox.Size.X/2
elseif S2 == "Top" or S2 == "Bottom" then
y -= hitbox.Size.Y/2
elseif S2 == "Front" or S2 == "Back" then
y -= hitbox.Size.Z/2
end
end
elseif nz ~= 0 and (nx == 0 and ny == 0) then
if nz > 0 then
print("Left")
if S3 == "Right" or S3 == "Left" then
z -= hitbox.Size.X/2
elseif S3 == "Top" or S3 == "Bottom" then
z -= hitbox.Size.Y/2
elseif S3 == "Front" or S3 == "Back" then
z -= hitbox.Size.Z/2
end
else
print("Right")
if S3 == "Right" or S3 == "Left" then
z += hitbox.Size.X/2
elseif S3 == "Top" or S3 == "Bottom" then
z += hitbox.Size.Y/2
elseif S3 == "Front" or S3 == "Back" then
z += hitbox.Size.Z/2
end
end
end
x = math.floor(x+0.5)
z = math.floor(z+0.5)
if itemTransparent.Parts.PrimaryPart.Size.X % 2 ~= 0 then
if posx > x then
x += 0.5
else
x -= 0.5
end
end
if itemTransparent.Parts.PrimaryPart.Size.Z % 2 ~= 0 then
if posz > z then
z += 0.5
else
z -= 0.5
end
end
folder.Parts:SetPrimaryPartCFrame(middle+Vector3.new(-x,y,-z))
local rogi = {
{1, 1, -1}, --top front right
{1, -1, -1}, --bottom front right
{-1, -1, -1},--bottom front left
{-1, 1, -1}, --top front left
{1, 1, 1}, --top back right
{1, -1, 1}, --bottom back right
{-1, -1, 1},--bottom back left
{-1, 1, 1} --top back left
}
local buildareas = plot.BuildAreas:GetChildren()
local boolTable = {}
for i,v in pairs(rogi) do
local CornerPos = hitbox.CFrame * CFrame.new(hitbox.Size.X/2 * v[1], hitbox.Size.Y/2 * v[2], hitbox.Size.Z/2 * v[3])
local region = RR3.new(CornerPos,Vector3.new(0,0,0))
local partsfound = region:FindPartsInRegion3WithWhiteList(buildareas)
local allpartsfound = region:FindPartsInRegion3()
if #partsfound == 0 then
table.insert(boolTable,false)
else
table.insert(boolTable,true)
end
for i,v in pairs(allpartsfound) do
if v.Name == "Hitbox" and v ~= hitbox then
table.insert(boolTable,false)
end
end
end
if not table.find(boolTable,false) then
canPlace = true
else
canPlace = false
end
refreshcolor()
end
local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
itemTransparent.Parent = workspace.Placing
RS.RenderStepped:connect(function()
local ray = Ray.new(mouse.UnitRay.Origin,mouse.UnitRay.Direction*1000)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = blacklist
local results = workspace:Raycast(ray.Origin,ray.Direction,params)
if results ~= nil then
local posX = plot.Middle.Position.X - results.Position.X
local posY = results.Position.Y - plot.Middle.Position.Y
local posZ = plot.Middle.Position.Z - results.Position.Z
local rayparams2 = RaycastParams.new()
rayparams2.FilterType = Enum.RaycastFilterType.Whitelist
rayparams2.FilterDescendantsInstances = {hitbox}
local ray1 = workspace:Raycast(hitbox.Position+Vector3.new(hitbox.Size.X,0,0),hitbox.Position,rayparams2)
local ray2 = workspace:Raycast(hitbox.Position+Vector3.new(0,hitbox.Size.Y,0),hitbox.Position,rayparams2)
local ray3 = workspace:Raycast(hitbox.Position+Vector3.new(0,0,hitbox.Size.Z),hitbox.Position,rayparams2)
local function getsurface(surfacenormal)
local normal = hitbox.CFrame:VectorToObjectSpace(surfacenormal)
local nx,ny,nz = normal.X,normal.Y,normal.Z
if nx ~= 0 and (ny == 0 and nz == 0) then
if nx > 0 then
print("Front")
return("Front")
else
print("Back")
return("Back")
end
elseif ny ~= 0 and (nx == 0 and nz == 0) then
if ny > 0 then
print("Top")
return("Top")
else
print("Bottom")
return("Bottom")
end
elseif nz ~= 0 and (nx == 0 and ny == 0) then
if nz > 0 then
print("Left")
return("Left")
else
print("Right")
return("Right")
end
end
end
local pos = Vector3.new(posX,posY,posZ)
snapToGrid(pos,plot.Middle.CFrame,itemTransparent,results.Position.X,results.Position.Z,results.Normal,results.Instance,getsurface(ray1.Normal),getsurface(ray2.Normal),getsurface(ray3.Normal))
end
wait()
end)
UIS.InputBegan:Connect(function(input)
print("input changed")
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("mouse button pressed")
if canPlace == true then
if plr.PlaceCooldown.Value == false then
if plr.Inventory.Structures:FindFirstChild(item.Name) ~= nil then
placeitem:FireServer(item.Name,itemTransparent.Parts.PrimaryPart.CFrame)
end
end
end
elseif input.KeyCode == Enum.KeyCode.R then
plot.Middle.Orientation += Vector3.new(0,90,0)
elseif input.KeyCode == Enum.KeyCode.T then
plot.Middle.Orientation += Vector3.new(90,0,0)
end
end)
end
return module