Why is this BlockCast not printing anything?

while true do
local R = RaycastParams.new()
R.FilterType = Enum.RaycastFilterType.Whitelist
R.FilterDescendantsInstances = {Enum.Material.Water}
local Start = script.Parent.Position
local End = script.Parent.Position - Vector3.new(0,10,0)
local Size = Vector3.new(3,3,3)
	local BlockCast = workspace:Blockcast(Start,Size,End,R)
	print(BlockCast)
	wait(0.5)
end

This is supposed to BlockCast to find terrain water. It’s inside a Dummy’s HumanoidRootPart, and it is a local script. I rolled the Dummy into water, and it nothing printed. (This my first time using BlockCast so sorry if these are silly questions)

I’d assume the script isn’t executing at all is why the print doesn’t show anything. Did you try changing to a server script? local scripts don’t run on the server so unless you are only creating the dummy on the client it would need a server script to run.

Okay yeah you’re right, it wasn’t doing anything. So now R.FilterDescendantsInstances = {Enum.Material.Water} says “Unable to cast value to Object”

You need to put in the physical Instances that you wish to filter not just an enumerated value. If there are parts in the workspace that are water you would add those or if you put all your water parts into a folder you could reference the folder as the Instance.

Could I reference “Terrain” and then specify only the water material?

Why FilterDescendantsInstances when you can just do:

R.IgnoreWater = false

while true do
local R = RaycastParams.new()
R.FilterType = Enum.RaycastFilterType.Whitelist
R.FilterDescendantsInstances = {game:GetService(“Workspace”).Terrain}
local Start = script.Parent.Position
local End = script.Parent.Position - Vector3.new(0, 10, 0)
local Size = Vector3.new(10, 10, 10)
local BlockCast = workspace:BlockCast(Start, End, Size, R)
if BlockCast then
print(“Water detected”)
else
print(“No water detected”)
end
wait(0.5)
end

I could do this, but is there a way I can still check with an if statement to make sure that the material is water?

You can try Terrain:ReadVoxel instead and getting Material[1][1][1]

I’ve never worked with this before, so I’ll need to look more into it. Also, I have an error with the BlockCast variable. I don’t know which variable, but it is “Unable to cast Vector3 to CoordinateFrame”

the start variable must a cframe and the direction must be CFrame.LookVector? idk probably I can’t explain it correctly btw you can try my code

--!strict

local RunService = game:GetService("RunService")

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid") :: Humanoid

local rootPart = humanoid.RootPart :: BasePart
if not rootPart then
	humanoid:GetPropertyChangedSignal("RootPart"):Wait()
	rootPart = humanoid.RootPart :: BasePart
end

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {character}

RunService.Stepped:Connect(function()
	local raycastResult = workspace:Blockcast(rootPart.CFrame, rootPart.Size, rootPart.CFrame.LookVector * 5, raycastParams)
	if raycastResult then
		print(raycastResult.Material)
	end
end)

This code can only check if the player is in front of the water

1 Like

What was trying to be achieved with humanoid.RootPart? Shouldn’t it just be script.Parent, as that is where the script is located?

the script is located in StarterCharacterScripts inside StarterPlayer when the script load the script will clone and place inside the character so the script.Parent should return the model of the character

humanoid.RootPart return the character’s RootPart


That is the rootPart

try this

while true do
local R = RaycastParams.new()
R.FilterType = Enum.RaycastFilterType.Whitelist
R.FilterDescendantsInstances = {game:GetService(“Workspace”).Terrain}
local Start = script.Parent.Position
local End = script.Parent.Position - Vector3.new(0, 10, 0)
local Size = Vector3.new(10, 10, 10)
local BlockCast = workspace:BlockCast(Start, End, Size, R)
if BlockCast then
print(“Water detected”)
else
print(“No water detected”)
end
wait(0.5)
end

Dude you just edit the code and it will still throw an error

1 Like

This code works now that I’ve put it in the correct place. Thank you for this. It’s gonna go towards a bucket filling system

1 Like

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