I am trying to create a grid based placement system. Whenever you try and place an object on the sides of another one, it goes inside of the object instead.
local replicated = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local ts = game:GetService("TweenService")
local buildings = replicated.Buildings
local functions = replicated.Functions
local player = players.LocalPlayer
local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
mouse.TargetFilter = workspace.Camera
local uis = game:GetService("UserInputService")
local selectedBuilding = nil
local rotation = 0
local gridSize = 4 -- in studs
function convertToGrid(cframe)
local xPos = cframe.Position.X
local zPos = cframe.Position.Z
local rounded1 = math.round(xPos/gridSize)*gridSize
local rounded2 = math.round(zPos/gridSize)*gridSize
cframe = Vector3.new(rounded1, cframe.Position.Y, rounded2)
return cframe
end
function createBuilding(building)
local building = replicated.Buildings:FindFirstChild(building)
if not building then warn("aint there homedog!!") return end
local building = building:Clone()
building.Parent = workspace.Camera
selectedBuilding = building
end
runService.RenderStepped:Connect(function()
if selectedBuilding then
local hit = mouse.Hit
local roundedPos = convertToGrid(hit)
local roundedY = math.round(hit.Position.Y/gridSize)*gridSize
local cframe = CFrame.new(Vector3.new(roundedPos.X, roundedY+selectedBuilding.PrimaryPart.Size.Y/2, roundedPos.Z)) * CFrame.Angles(0, math.rad(rotation), 0)
selectedBuilding:SetPrimaryPartCFrame(cframe)
end
end)
uis.InputBegan:Connect(function(input, proc)
if input then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if selectedBuilding then
print("YEAHJ")
local success = functions.SpawnBuilding:InvokeServer(selectedBuilding.Name, selectedBuilding.PrimaryPart.CFrame)
if success then
end
end
elseif input.KeyCode == Enum.KeyCode.R then
if selectedBuilding then
rotation += 90
end
end
end
end)
script.Parent.debug.MouseButton1Click:Connect(function()
createBuilding("example")
end)