So, I’m trying to make a building system in Roblox Studio, so that when you hold a tool you can place a block. One thing I didn’t realize was so important (and what created the issue) is aligning parts depending on the surface. For example, if you’re building on the ground or the top of a part, you’d expect the part to be built centered above the cursor, so that the part is placed on the ground, not in it. However, when you’re placing a part on the side of another part, you’d expect that the part being created is placed with it centered on the cursor, right up against the other part. So I made code to do this (seen as the block of if-statements in the code below), using the mouse.Hit.TargetSurface. However, the issue comes when adding part rotation. When the part is rotated, suddenly all the checks in the block of if-statements become incorrect, leading to the part not being aligned properly with other parts.
Here is my code, a local script parented to the tool (note: the player.Inventory.Wood is an IntValue storing how much wood the player has, since building requires wood.):
local player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
while (not player.Character) or (not player:FindFirstChild("Inventory")) do
wait()
end
local hover = game.ReplicatedStorage.WoodCube:Clone()
hover.Parent = game.Workspace
hover.BrickColor = BrickColor.new("Electric blue")
hover.Transparency = 0.5
hover.CanCollide = false
hover.Transparency = 1
local mouse = player:GetMouse()
script.Parent.Equipped:Connect(function()
hover.Transparency = 0.5
mouse.TargetFilter = hover
while script.Parent.Parent:FindFirstChild("Humanoid") do
if (not player.Inventory:FindFirstChild("Wood")) or not ((hover.Position - script.Parent.Parent.HumanoidRootPart.Position).magnitude <= 30) then
hover.BrickColor = BrickColor.new("Bright red")
else
hover.BrickColor = BrickColor.new("Electric blue")
end
if mouse.TargetSurface.Name == "Top" then
hover.Position = mouse.Hit.Position + Vector3.new(0, (hover.Size.Y / 2), 0)
elseif mouse.TargetSurface.Name == "Bottom" then
hover.Position = mouse.Hit.Position + Vector3.new(0, -(hover.Size.Y / 2), 0)
elseif mouse.TargetSurface.Name == "Left" then
hover.Position = mouse.Hit.Position + Vector3.new(-(hover.Size.X / 2), 0, 0)
elseif mouse.TargetSurface.Name == "Right" then
hover.Position = mouse.Hit.Position + Vector3.new((hover.Size.X / 2), 0, 0)
elseif mouse.TargetSurface.Name == "Back" then
hover.Position = mouse.Hit.Position + Vector3.new(0, 0, (hover.Size.Z / 2))
elseif mouse.TargetSurface.Name == "Front" then
hover.Position = mouse.Hit.Position + Vector3.new(0, 0, -(hover.Size.Z / 2))
else
hover.Position = mouse.Hit.Position + Vector3.new(0, (hover.Size.Y / 2), 0)
end
wait()
end
hover.Transparency = 1
end)
mouse.Button1Down:Connect(function()
if (script.Parent.Parent:FindFirstChild("Humanoid")) and (player.Inventory:FindFirstChild("Wood")) and (not (mouse.Target == nil)) then
if (hover.Position - script.Parent.Parent.HumanoidRootPart.Position).magnitude <= 30 then
game.ReplicatedStorage.PlaceObject:FireServer(hover.CFrame)
end
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
hover.Rotation += Vector3.new(0, 90, 0)
end
end)
I need to find a way to fix my issue, but I can’t think of anything. Any help is appreciated.
p.s. I know my code is far from optimal, feel free to offer suggestions on fixing anything that may be game-breaking.