Get nearest block

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like to be able to get the nearest block infront of the player.

  2. What is the issue? Include screenshots / videos if possible!
    I have the script to get the direction the player is facing, but idk how to get the nearest block in that direction.

local player = game.Players.LocalPlayer 
local Humanoid = game.Workspace:WaitForChild(player.Name):WaitForChild("HumanoidRootPart")

while true do
	local direction = Humanoid.CFrame.LookVector
	print(direction)

	-- Wait for 0.1 seconds before checking again
	wait(0.1)
end
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried searching around, found nothing
1 Like

Either use GetPartBoundsInBox or one of the related methods, or use a spatial data structure for the blocks.

1 Like

–Code above this line is not mine, but I would like to have it in the script.

What have you tried so far?
I have tried to find the answer on google, but I couldnt find anything.

I don’t know what else to write, so here is a picture of a cat:

One method is to use the FindPartOnRay function to get the part closest to the player within a certain range.
local player = game.Players.LocalPlayer
local humanoid = player.Character:FindFirstChild(“HumanoidRootPart”)
local part = workspace:FindPartOnRay(Ray.new(humanoid.CFrame.p, humanoid.CFrame.LookVector), player.Character)

The Ray takes two arguments, the start of the ray and the direction of the ray. You can use CFrame.p to get the position of the humanoid. The FindPartOnRay function will return the part closest to the player within the range specified. You may also need to filter out parts that are owned by the player.