Allow only certain tagged objects to be Ray casted and accepted

I’ve successfully created a working “Vault” Mechanic. Im currently trying to only let in objects that are tagged with “Vault” but the code was forked from some random person and Idk how to transfer it to normal Ray casting.

Example code of what I want:

-- RayParams
local rayFilter = RaycastParams.new()
rayFilter.CollisionGroup = "Ledge"
rayFilter.FilterDescendantsInstances ={TAGGEDOBJECTS} -- HEY LOOK OVER HERE

-- Ray Instance
local ray = workspace:Raycast(rightPosition, RootPart.CFrame.LookVector * 5, rayFilter)

Vault Function (Performs Vault, Put in a Runservice connection to continue checking):

function VaultModule:Vault()
	
	local Ray = Ray.new(RootPart.CFrame.p, RootPart.CFrame.LookVector * 3)
	
	local RayResult = workspace:FindPartOnRay(Ray, Character, true)
	
	if RayResult and RayResult:IsA('BasePart') then

		if RayResult.Size.Y < 4.1 then

			if (Head.Position.Y - RayResult.Position.Y) >= 2 then

				if Humanoid.FloorMaterial == Enum.Material.Air then return end
				
				local RandomNumber = math.random(5)
				local VaultAnimation = VaultTable['Vault'..RandomNumber]

				VaultAnimation:Play()	
				VaultAnimation:AdjustSpeed(1)

				VaultSound:Play()
			end

		end

	end

end
1 Like

Just tweak the RaycastParams slightly, and you’ll be good to go.

local cs = game:GetService("CollectionService")

local params = RaycastParams.new()
params.FilterDescendantsInstances = cs:GetTagged("Vault") --allow tagged
params.FilterType = Enum.RaycastFilterType.Include --only count tagged parts

--now cast
local hit = workspace:Raycast(RootPart.CFrame.Position, RootPart.CFrame.LookVector * 3)

if hit then
    print(`Hit object: {hit.Instance.Name} at position: {hit.Position}`)
end
4 Likes

Your code appears to be using the very old FindPartOnRay method instead of :Raycast. You should update it, and then you can pass the RaycastParams into it as well.

1 Like

Small Problem. It doesnt find a part on ray. How do I do this?

	local RayCastParams = RaycastParams.new()
	RayCastParams.FilterDescendantsInstances = CollectionService:GetTagged("Vault") --allow tagged
	RayCastParams.FilterType = Enum.RaycastFilterType.Include --only count tagged parts
	
	local RayResult = workspace:Raycast(RootPart.CFrame.Position, RootPart.CFrame.LookVector * RayCastRange, RayCastParams)
	
	if RayResult then

		if RayResult.Size.Y < 4.1 then

			if (Head.Position.Y - RayResult.Position.Y) >= 2 then

Error:
image

1 Like

Yeah, Like I said

Problem is it also relies on logic of the function “FindPartOnRay”.

Dont know how to transfer this sadly.

1 Like

That doesn’t look like it’s not finding the ray. That just looks like an incorrect index. If you want the part hit, you can do RaycastResult.Instance, so try this:

print(RayResult and "Ray hit part: "..RayResult.Instance or "Raycast did not hit part.")

That should tell you if and what part the raycast result hit.

Hopefully this converts it:

function VaultModule:Vault()
	local Filter = RaycastParams.new()
	Filter.FilterType = Enum.RaycastFilterType.Include
	Filter.FilterDescendantsInstances = game:GetService("CollectionService"):GetTagged("Vault")
	
	local RayResult = workspace:Raycast(RootPart.Position, RootPart.CFrame.LookVector * 3, Filter)

	if RayResult and RayResult.Instance and RayResult.Instance:IsA("BasePart") then
		
		if RayResult.Instance.Size.Y < 4.1 then

			if (Head.Position.Y - RayResult.Position.Y) >= 2 then

				if Humanoid.FloorMaterial == Enum.Material.Air then return end

				local RandomNumber = math.random(5)
				local VaultAnimation = VaultTable['Vault'..RandomNumber]

				VaultAnimation:Play()	
				VaultAnimation:AdjustSpeed(1)

				VaultSound:Play()
			end
		end
	end
end

Thank you for this, Appreciate it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.