Raycasting detecting part through a part? (UPDATE: it now just doesn't detect anything)

I’m making a 2d mine game and I want that you can’t mine stone through a stone
also quick little note I am very new to raycasting maybe I just don’t understand how it works

I’ve made the raycasting system and to test it I changed the Transparency of the hitPart to .5 but for some reason the hitpart is through another part https://gyazo.com/5af16cce7f9bc852617a00efa6066331

here is my code:

Localscript:

local ray = Camera:ScreenPointToRay(Mouse.X, Mouse.Y)
local IsBehindObject = SelectedPart:InvokeServer(Mouse.Target, ray)

Script:

SelectedPart.OnServerInvoke = function(player, part, ray)
	local character = player.Character or nil
	if character == nil then return true end
	local HumanoidRootPart = character.HumanoidRootPart or character.WaitForChild("HumanoidRootPart")
	
	local rayOrigin = Vector3.new(HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, 0)
	local length = 1000
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, ray.Direction * length, raycastParams)
	
	if raycastResult then
		local hitPart = raycastResult.Instance
		hitPart.Transparency = .5
	end
	return false
end

You didn’t set your newRay’s origin to rayOrigin, you used the original origin which doesn’t correct for the HumanoidRootPart

It might be a part of the problem but now I have a new problem that there just is no result

Set the Z axis argument of the rayOrigin to the HumanoidRootPart Z position

Also, screenpointtoray is a Vector2, not a Vector3, no need for the Z axis on that one

still getting no results (apparently this message needs to be longer)

that’s depth not z (I removed that too and no result too btw)

The Z-axis refers to the depth of an object, and what is the advantage to using screenpointtoray?

well I don’t know what to use other then screenpointtoray

SelectedPart.OnServerInvoke = function(player, part) --No more passing ray through
local character = player.Character or nil
if character == nil then return true end
local HRP = character.HumanoidRootPart or character.WaitForChild("HumanoidRootPart")

local newRay = Ray.new(Vector3.new(HRP.Position.X, HRP.Position.Y, HRP.Position.Z), Vector3.new(part.Position.X, part.Position.Y, part.Position.Z))
local length = 1000

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(newRay.Origin, newRay.Direction * length, raycastParams)

if raycastResult then
	local hitPart = raycastResult.Instance
            if hitPart == part then
	    hitPart.Transparency = .5
            end
end
return false
end

well guess today is the day where I’m getting spoon fed, anyways I will try it now

you know that you can’t just get the position of two parts you need to get the position of the origin and then the direction to where the ray will cast

Sorry, I’m busy with something else right now, you can fix it by just converting the position of the mouse.target part to objectspace or get the direction by subtracting the Vector3’s of the target part and rootpart

How do you invoke the server, can we see the local script please.