Trying to make an "advanced" building tool

  1. What do you want to achieve?

I am trying to make a game, where players using different parts, build structures or vehicles in order to win.

The thing I am trying to do RN is the building tool. I imagined it as an individual tool for each part (Players find parts around the map and have to pick them up). The picked up part works as a tool.

  1. What is the issue?

While I made a simple script for picking up and placing the newly acquired part, I ran into an issue. The parts work rather like “blocks”. If you try to place 1 part on another tilted or rotated part, the new part will not snap its orientation to the part you are about to place it on.

  1. What solutions have you tried so far?

I tried to make newly placed part match the Orientation property of the part that you are placing new part on. However it does not work really well with parts that are not cubical. I thought if raycasting would help, but I am not so experienced with it. This is why I finally decided to ask for help. This is my first ever post, so any help is appreciated.

Now I will give more details… So the tool itself is a simple tool with handle and a part that is gonna be placed attached to it. In the tool there is also a “holo” of a part (its half transperent clone of the part that is anchored, cant be collided with and leaves no shadows). When left mouse click is initiated, the part in tool is being moved to workspace and it’s holo position, and the tool is being destroyed. I use Mouse().Hit.Position to get the position of mouse.

If you need more details, ill try to make screenshots of code and such, but right now im just in confusion :face_with_head_bandage:

3 Likes

Still have not found the answer, but I found “Build a boat for treasure” building tool, kind of similar in what I am trying to achieve. No matter how rotated or tilted it is, it always is distanced from the part that you are trying to place it on. May be somebody knows how to achieve this?

image

And if you use “Match rotation” in this setting, it is exactly what I need.

1 Like

Can you show code and/or a video of you attempting to recreate what building a boat did?

1 Like

Yeah sure, one moment. But to let you know, the code itself is not perfect as due to my attempts to make it work like I want I changed a lot of its components.

**
image
**
Thats the beginning of the code. It determines the position of mouse in 3D space and then rotates the block that same orientation as the block that mouse is aimed at.

And to make the “grid-like” placement, I just take individual X Y Z values of position and round them using math.round()

Time to make a little update on what I have done so far…

While I still did not find a solution for my question, I decided to make something more simple.

The code below gives the player a simple building tool (place the block). The code is put in local script, which is located inside a tool that has a “Block”, “BlockHolo” and a “Handle” that is welded to the “Block”.

local ContextActionService = game:GetService('ContextActionService')
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

local tool = script.Parent
local Block = tool.Block
local Holo = tool.BlockHolo

local function DisplayAndPlace(actionName, inputState, inputObject) 
	
	Player:GetMouse().Move:Connect(function()
		
		local mousePosition = Player:GetMouse().Hit.Position  -- Determine the position of mouse
		
		local mousePositionX = math.round(mousePosition.X) -- Round X value of mouse position
		local mousePositionY = math.round(mousePosition.Y) -- Round Y value of mouse position
		local mousePositionZ = math.round(mousePosition.Z) -- Round Z value of mouse position
		
		local roundedMousePosition = Vector3.new(mousePositionX, mousePositionY, mousePositionZ) -- Rounded mouse position
		
		local targetSurface = Player:GetMouse().TargetSurface.Name -- Check at which surface of a part your mouse is pointed at
		local target = Player:GetMouse().Target  -- Check the part your mouse is pointed at
		
		local function CreateBlockPosition() -- Determines where the new block position should be, depending on the targetSurface
			if targetSurface == "Front" then
				Holo.Position = roundedMousePosition + Vector3.new(0, 0, -1)
			elseif targetSurface == "Back" then
				Holo.Position = roundedMousePosition + Vector3.new(0, 0, 1)
			elseif targetSurface == "Left" then
				Holo.Position = roundedMousePosition + Vector3.new(-1, 0, 0)
			elseif targetSurface == "Right" then
				Holo.Position = roundedMousePosition + Vector3.new(1, 0, 0)
			elseif targetSurface == "Top" then
				Holo.Position = roundedMousePosition + Vector3.new(0, 1, 0)
			elseif targetSurface == "Bottom" then
				Holo.Position = roundedMousePosition + Vector3.new(0, -1, 0)
			end
		end
		
		CreateBlockPosition()
		
	end)
	
	if inputState == Enum.UserInputState.Begin then -- Clone a block from a tool, move it into workspace and place it at the Holo position when the left mouse button is pressed
		local clone = Block:Clone()
		clone.Parent = workspace
		clone.Position = Holo.Position
		clone.Orientation = Holo.Orientation
		clone.CastShadow = true
		clone.Anchored = true
		clone.CanCollide = true
		clone.Transparency = 0
	end
		
end

ContextActionService:BindAction("BlahBlah", DisplayAndPlace, false, Enum.UserInputType.MouseButton1)

While the tool itself functions quite fine there are still some requirement for it to work properly. The Parts in the world, as well as the “BlockHolo” should have their “Orientation” property set to (0, 0, 0). The block also has to be 2 by 2 by 2 in size.

As of issues the tool does not change the “BlockHolo” position until the first input, and the player is able to place blocks even without tool being equipped.

In the end the tool turned out to be quite undeveloped, but if you have any suggestions or ideas on how I can fix and improve it, I would be really happy.

It has been a long time since last update. But I’ve tried to make the tool work better, and I think I finally managed to. Now my script uses Look/Up/Right Vectors to move the part away from the surface the mouse is pointed at. This gives an amazing opportunity to implement block rotation. I would love to mention this person, who helped me learn how to properly identify surface, which made this advancement possible.


Additionally there is a screenshot of a code. I removed old messy parts but with that also removed place function, to upgrade it in future.

Small update on progress:
image
I implemented rotating and placing using context action service. Planning to making a new holo being the somewhat “stairs” like to see if it works properly with that, and if it is even possible to use models instead of a singular part