A way to not create a instance when not detecting a basepart

Hello I’m trying to make a rock effect but the problem is every time it’s the edge a part still spawns and will break the script. I tried to look in the dev forum but can’t find anything. Help would be great :smiley:

What I got:

What I wanted:

local rock = Instance.new("Part")
rock.Anchored = true
rock.CanCollide = false
rock.Size = Vector3.new(SizeZ,SizeZ,SizeZ)
rock.CFrame = obj.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(Ori),0) * CFrame.new(Room,2,-Room)
rock.Orientation = Vector3.new(math.random(-20,20),math.random(-20,20),math.random(-20,20))
rock.Parent = workspace.FXDebris
		
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.IgnoreWater = true
		
local result = workspace:Raycast(rock.Position,Vector3.new(0,-h,0),rayParams)
		
if result then
	rock.Position = (result.Position - Vector3.new(0,SizeZ,0))
	rock.Color = result.Instance.Color
	rock.Material = result.Material
	TweenService:Create(rock,TweenInfo.new(1), {Position = result.Position}):Play()		delay(T,function()
		TweenService:Create(rock,TweenInfo.new(1), {Position = result.Position - Vector3.new(0,SizeZ,0)}):Play()
	end)
end

Quite simple, move the part of your code where the parts get created into the result check.

I have tried that but it doesn’t spread out it could work but I will have to change the obj.Parent.CFrame

Hello I just fixed my own problem by just changing the rock.CFrame into a variable and get the rock and others to the result part.