Raycast not raycasting

Hello guys, I recently wanted to use raycast with FilterType of Include (like shown below):

RayParams2.FilterType = Enum.RaycastFilterType.Include
RayParams2.FilterDescendantsInstances = {game.Workspace.PetCorrectionFolder:WaitForChild("PetWalkingPart")}

local RayResult1 = workspace:Blockcast(Pet.PrimaryPart.CFrame, Pet.PrimaryPart.Size, Vector3.new(0, -10, 0), RayParams2)

and for some reason, even with a very big part and very clear collision, it doesnt give me any resoults. I tried changing almost every variable and it feels like its just broken on roblox side but I’m just probablly dumb and dont see a mistake somewhere.

If anyone knows why is it happening, pls help :pray:

thx

2 Likes

Can I get more information about RayParams2? Just send me the whole code. I may be able to help on this one!

2 Likes

well, there is not much more to it but here you have the whole code:

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local PetsFolder = game.Workspace:WaitForChild("PlayerPets")

local Spacing = 5
local PetSize = 3
local MaxClimbHeight = 5

local RayParams = RaycastParams.new()
local RayParams2 = RaycastParams.new()
local RayDirection = Vector3.new(0, -100, 0)

local function RearrangeTables(Pets, Rows, MaxRowCapacity)
	table.clear(Rows)
	
	local AOR = math.ceil(#Pets / MaxRowCapacity)
	
	for i = 1, AOR do
		table.insert(Rows, {})
	end
	
	for i, v in Pets do
		local Row = Rows[math.ceil(i / MaxRowCapacity)]
		table.insert(Row, v)
	end
end

local function GetRowWidth(Row, Pet)
	if Pet ~= nil then
		local SpacingBetweenPets = Spacing - Pet.PrimaryPart.Size.X
		local RowWidth = 0
		
		if #Row == 1 then
			return 0
		end
		
		for i, v in Row do
			if i ~= #Row then
				RowWidth += Pet.PrimaryPart.Size.X + SpacingBetweenPets
			else
				RowWidth += Pet.PrimaryPart.Size.X
			end
		end
		
		return RowWidth
	end
end

RunService.RenderStepped:Connect(function(DeltaTime)
	for i, PlayerPetFolder in pairs(PetsFolder:GetChildren()) do
		if not Players:FindFirstChild(PlayerPetFolder.Name) then return end
		
		local Character = Players[PlayerPetFolder.Name].Character or Players[PlayerPetFolder.Name].CharacterAdded:Wait()
		local HRP = Character:WaitForChild("HumanoidRootPart")
		local Humanoid = Character:WaitForChild("Humanoid")
		
		if Character == nil or HRP == nil or Humanoid == nil then return end
		
		local Pets = {}
		local Rows = {}
		
		for _, v in pairs(PlayerPetFolder:GetChildren()) do
			table.insert(Pets, v)
		end
		
		RayParams.FilterDescendantsInstances = {Character, PetsFolder}
		RayParams2.FilterType = Enum.RaycastFilterType.Include
		RayParams2.FilterDescendantsInstances = {game.Workspace.PetCorrectionFolder:WaitForChild("PetWalkingPart")}
		
		local MaxRowCapacity = math.ceil(math.sqrt(#Pets))
		
		RearrangeTables(Pets, Rows, MaxRowCapacity)
		
		for i, Pet in Pets do
			local RowIndex = math.ceil(i / MaxRowCapacity)
			local Row = Rows[RowIndex]
			local RowWidth = GetRowWidth(Row, Pet)
			
			local XOffset = #Row == 1 and 0 or RowWidth / 2 - Pet.PrimaryPart.Size.X / 2
			local X = (table.find(Row, Pet) - 1) * Spacing
			local Z = RowIndex * Spacing
			local Y = 0
						
			local RayResult1 = workspace:Blockcast(Pet.PrimaryPart.CFrame, Pet.PrimaryPart.Size, Vector3.new(0, -10, 0), RayParams2)
						
			if RayResult1 then
				print(RayResult1)
				if RayResult1.Instance.Parent.Name == "PetCorrectionFolder" then
					MaxClimbHeight = 100
				end
			else 
				MaxClimbHeight = 5
			end--]]
			
			local RayResult = workspace:Blockcast(Pet.PrimaryPart.CFrame + Vector3.new(0, MaxClimbHeight, 0), Pet.PrimaryPart.Size, RayDirection, RayParams)
			
			if RayResult then
				Y = RayResult.Position.Y + Pet.PrimaryPart.Size.Y / 2
			else -- Using HRP
				print("used HRP")
				Y = HRP.CFrame.Y
			end
			
			--local TargetCFrame = CFrame.new(HRP.CFrame.X, 0, HRP.CFrame.Z) * HRP.CFrame.Rotation * CFrame.new(X - XOffset, (Y + 1) + (math.sin(tick()) / 3), Z)
			local TargetCFrame = CFrame.new(HRP.CFrame.X, 0, HRP.CFrame.Z) * HRP.CFrame.Rotation * CFrame.new(X - XOffset, Y, Z)
			
			Pet.PrimaryPart.CFrame = Pet.PrimaryPart.CFrame:Lerp(TargetCFrame, 0.1)
			
			if Pet.PrimaryPart.CFrame.Y <= -100 then
				Pet.PrimaryPart.CFrame = CFrame.new(Pet.PrimaryPart.CFrame.X, HRP.CFrame.Y, Pet.PrimaryPart.CFrame.Z)
			end
		end
	end
end)
2 Likes

Maybe you can print the values of what is being put in the ray cast. Also, Maybe check the direction, I could be in a different direction or what. I don’t understand what is supposed to happen. Maybe make a script where you just make the ray cast and test to see if it works and try to add it into the main script. I would try all of these.

1 Like

the initial idea was to detect when the pet is in a designated area but for some reason it doesnt detect it at all. It works perfectly fine with detecting stuff if you set the FilterType to Exclude, not Include

1 Like

Maybe check the thing you are filtering “game.Workspace.PetCorrectionFolder:WaitForChild(“PetWalkingPart”)” Maybe it can be the wrong thing you are trying to detect or something. Try to see if it could be nil.

nvm I fixed it, not sure how but I assume the raycast wasn’t hitting the part cus it wasn’t below it but in it I think

Great! Did I at least help you? Good luck with your pet game!

a bit but I mostly just tried some random stuff and it worked :eyes:

oh an btw, do you know if I can make Streaming Enabled ignore some parts I want?

If you set a model’s ModelStreamingMode property to “Persistent” it should never stream out, assuming that’s what you want. This applies to everything inside the model

I found a way around it but this would prob work too, thx :+1: