Anyone know how to make a Script to make a function happen when the players HumanoidRootPart is facing a block?

Hello, I’m trying to make it where if your players HumanoidRootPart is facing straight towards a block something happens, Anyone know how to do this?

Use raycasting.

i have this script that works but u have to touch the block to make it work do u know how to make it range instead?

local char = player.Character
local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function(step)
	local ray = Ray.new(char.Head.Position, ((char.Head.CFrame + char.Head.CFrame.LookVector * 2) - char.Head.Position).Position.Unit)
	local ignoreList = char:GetChildren()

	local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

	if hit then
		print("Hit")
	end	
end)

Try this It extends the ray by multiplying the length by 300:

local ray = Ray.new(char.Head.Position, ((char.Head.CFrame + char.Head.CFrame.LookVector * 2) - char.Head.Position).Position.Unit * 300) -- add  * 300

it works thank youuuuuuuuuuuuuu

1 Like

and last thing how would i make it for certain parts only?

Try this, I think it will work. It uses the whitelist feature on raycasts:

local char = player.Character
local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function(step)
	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = {script.Parent.Parent}
	RaycastParameters.FilterType = Enum.RaycastFilterType.Whitelist()--PUT MODEL IN HERE, like: game.Workspace.ExampleModel if your part isnt on a model put it in one
	local Direction = ((char.Head.CFrame + char.Head.CFrame.LookVector * 2) - char.Head.Position).Position.Unit * 300
	local raycastresult = workspace:Raycast(char.Head.Position,Direction, RaycastParameters)
	if raycastresult then
		local hitpart = raycastresult.Instance
		print("Hit")
	end
end)

i did this

RaycastParameters.FilterType = Enum.RaycastFilterType.Whitelist(game.Workspace.CantTouch.Part)

and got a error saying this

Workspace.Cosnez.LocalScript:8: attempt to call a EnumItem value

nevermind i got it, thanks 30 letterssss

1 Like

Make sure your just referencing the model here:
RaycastParameters.FilterType = Enum.RaycastFilterType.Whitelist(game.Workspace.CantTouch.Part)
you don’t need to say .part if cant touch is the model and part is the part