Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how can i make blocks in a grid place sideways?

i tried making a minecraft like building system on a grid, it works fine, but its not what i want. it only places up and not sideways, and i dont know how to find the face a mouse is clicking on. right now i have some pretty simple code to make it so when you right click a block on the grid it appears 3 blocks above it (the block size is 3,3,3) i tried ray tracing but it was too COMPLEX for me, and it kept erroring. heres my original script:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local block = game.Lighting:FindFirstChild("block_types"):FindFirstChild("plastic")
local val
local down

function dupeblock()
	if mouse.Target ~= nil and mouse.Target.Parent == workspace.grid or workspace.blocks then
		local cblock = block:Clone()
		cblock.Parent = workspace.blocks
		cblock.Anchored = true
		down = true		
	end
end

mouse.Button2Down:connect(dupeblock)

function placeblock()
	if mouse.Target ~= nil and mouse.Target.Parent == workspace.grid or workspace.blocks then
		local nblock = workspace.blocks:FindFirstChild("plastic")
		nblock.CFrame = CFrame.new(mouse.Target.Position)+Vector3.new(0,3,0)
		val = math.floor(math.random())
		nblock.Name = nblock.Name..val
	end
end

mouse.Button2Up:connect(placeblock)

2 comments

1

please put your code in a code block so it can be easier to read.

0

ok

1 answer

✓ Accepted answer

Ive worked on a similar script, and ray tracing is your best bet to get the surface face of a block. There is a third property returned when calling "FindPartOnRay" that is the surfaceNormal of a part. If you multiply this surface normal by half the size of the block, it will give you the offset of your block. Here is some pseudo code to help:

local WS = game.Workspace;

	--> 1 Create the ray
	local unitRay = WS.CurrentCamera:ScreenPointToRay(mouse.x, mouse.y);
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * 500);
	local hit, pos, normal = WS:FindPartOnRay(ray, part);--How a ray is created. Only the surfaceNormal is important here.

	--> 2 Get the offset
	local offset = normal * part.Size/2;--THe normal is only one stud long, so multiplying it by half the part size will position your part correctly.
	local newPos= pos + offset;--Add the mousePosition to the offset

	--> 3 Assign the new position
	part.Position = newPos;

This discussion is preserved as it was. New answers, comments, and votes are closed.