Raycasting can't find client sided parts

I am trying to make a function that textures a Minecraft like terrain chunk by only texturing the sides of the blocks that can be seen (for optimization, as rendering unseen faces of blocks would decrease performance).

What I’m trying to do is raycast in six directions from each block, check if a block is blocking that direction, and adding a texture to that direction’s face if not. This is all being done on the client.

Every single RaycastResult returns nil, and the raycast can’t seem to recognize any of the blocks. Before I added the raycast params, the raycast did once detect my character on the server. I believe that the raycast isn’t hitting these blocks because the blocks are all loaded on the client, though I may be wrong.

Is there anyway I can fix this? Or is there a better way to handle this?
These blocks have CanQuery on, Transparent attribute off, and printing the RaycastResult prints nil.

All interior faces shown, top faces removed

Raycast Params

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Include
rayParams.FilterDescendantsInstances = {workspace.Worldspace} -- where the blocks are held

Texture Function

function textureFace(result: 'RaycastResult', block:'Block', face:'Enum.NormalId',name:'Face Name')
	local old = block:FindFirstChild(name) -- checking if texture was already there
	print(result)
	if not result or result.Instance:GetAttribute('Transparent') == true then -- if not being blocked
		if not old then -- if texture wasn't there already
			local new = texture:Clone()
			new.Face = face
			new.Name = name -- giving texture a name for ease of checking for old texture
			new.Parent = block
		end
	elseif old then
		old:Destroy() -- eliminating texture if covered by block
	end
end
function textureChunk(chunk: 'Chunk')
	for _, block in pairs(chunk:GetChildren()) do
		textureFace(workspace:Raycast(block.Position,Vector3.new(0,1,0),rayParams),block,Enum.NormalId.Top,'top')
		textureFace(workspace:Raycast(block.Position,Vector3.new(0,-1,0),rayParams),block,Enum.NormalId.Bottom,'bottom')
		textureFace(workspace:Raycast(block.Position,Vector3.new(0,0,-1),rayParams),block,Enum.NormalId.Front,'front')
		textureFace(workspace:Raycast(block.Position,Vector3.new(0,1,1),rayParams),block,Enum.NormalId.Back,'back')
		textureFace(workspace:Raycast(block.Position,Vector3.new(1,0,0),rayParams),block,Enum.NormalId.Left,'left')
		textureFace(workspace:Raycast(block.Position,Vector3.new(-1,0,0),rayParams),block,Enum.NormalId.Right,'right')
	end
end

Did you say you’re raycasting from the server onto client instances?

2 Likes

I don’t think that raycasting from the server will work on instances that only exists on the client…

Sorry if I was unclear, all of the code is on the client

Perhaps it’s only seaching for a single stud in each direction from the block’s origin position, try increasing the vector’s magnitude

Vector3.new(0,0,-1) * [additional distance]

It didn’t work. All of the blocks are one stud, so that’s why I didn’t add any magnitude

		textureFace(workspace:Raycast(block.Position,Vector3.new(0,1,0)*100,rayParams),block,Enum.NormalId.Top,'top')
		textureFace(workspace:Raycast(block.Position,Vector3.new(0,-1,0)*100,rayParams),block,Enum.NormalId.Bottom,'bottom')
		textureFace(workspace:Raycast(block.Position,Vector3.new(0,0,-1)*100,rayParams),block,Enum.NormalId.Front,'front')
		textureFace(workspace:Raycast(block.Position,Vector3.new(0,1,1)*100,rayParams),block,Enum.NormalId.Back,'back')
		textureFace(workspace:Raycast(block.Position,Vector3.new(1,0,0)*100,rayParams),block,Enum.NormalId.Left,'left')
		textureFace(workspace:Raycast(block.Position,Vector3.new(-1,0,0)*100,rayParams),block,Enum.NormalId.Right,'right')

I haven’t figured out the raycasting approach, so I just did something else to find the neighbors of a block. Now it textures the chunk fine

If someone could tell me why it didn’t work, I would appreciate it

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