Mouse raycasting placement system broken

My mouse raycasting placement system works well. But when i move the mouse up to the sky, this thing happens.
No error.
How can i fix this?

robloxapp-20230414-1716176.wmv (1.5 MB)

im sorry for the laggy vid

this is my current code.

local function raycast(origin, direction)
	local raycastResult = workspace:Raycast(origin, direction) -- Don't worry about the origin and direction error
	if raycastResult and raycastResult.Instance then
		return raycastResult.Position
	end
	return nil
end

You’re not returning a position vector when you don’t hit anything - which happens when you’re shooting into the sky. The position vector when the raycast doesn’t hit a part is equal to origin + direction.

The last line, return nil means it returns nil if the ray hits nothing. And i said there’s no error in my code.

I think the ray hits the object you want to place, so you can drag it up by holding your mouse on the object. Try filtering it out using:

local function raycast(origin, direction, blacklist)
	local raycastparams = RaycastParams.new()
	raycastparams.FilterDescendantsInstances = blacklist
	local raycastResult = workspace:Raycast(origin, direction, raycastparams)
	if raycastResult and raycastResult.Instance then
		return raycastResult.Position
	end
	return nil
end

if the object is a model you can use

object:GetDescendants()

as the blacklist parameter.

Thank you so much. It works good now.

1 Like

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