Unable to cast CoordinateFrame to Vector3

I have a script. The error is the title.

local floor: Vector3 = workspace:Raycast(position, Vector3.new(0,-5,0)).Position

Thats the piece of code where the error happens.
There’s another unsolved thing I need help on so it would be nice if you check it out.

Check to make sure you are actually getting a valid return on your raycast.

local result = workspace:Raycast(… )
if result ~= nil then

end

something like that.

Didn’t work. The problem is the raycast I’m sure.

Do you need the whole script? (limit)

When you say, ‘didn’t work’ do you mean…
That you tried it with an ‘if’ statement but it never reached the inside of the if?
Or do you mean that it gave an error still?

Also, yes, more code might be helpful.

Ok.

Tree.New = function(position: CFrame, tree: BasePart)
	local newTree: BasePart = tree:Clone()
	newTree.Size = Vector3.new(tree:GetAttribute("Thickness"),0.5,tree:GetAttribute("Thickness"))
	local treeInside: BasePart = newTree:FindFirstChild("Inside") :: BasePart
	treeInside.Size = Vector3.new(tree:GetAttribute("Thickness") - 0.1,0.5,tree:GetAttribute("Thickness") - 0.1)
	local floor: Vector3 = workspace:Raycast(position, Vector3.new(0,-5,0)).Position
	if(floor) then
		floor -= Vector3.new(0,0.51,0)
		newTree.Position = floor
		local treeTiltValue: number = tree:GetAttribute("Tilt")
		local rx, ry, rz: number = math.random(-treeTiltValue,treeTiltValue), math.random(-treeTiltValue,treeTiltValue), math.random(-treeTiltValue,treeTiltValue)
		newTree.Rotation = Vector3.new(rx, ry, rz)
	else
		error("Raycast failed!")
	end
end

I see you have ‘position’ coming in as a CFrame
However, I think the Raycast for the first parameter needs a Vector3

so maybe

workspace:Raycast(position.Position, Vector3.new(0,-5,0))

Also, I would do this…

	local result = workspace:Raycast(position.Position, Vector3.new(0,-5,0))
	if (result and result.Position) then
		local floor = result.Position
                floor -= Vector3.new(0,0.51,0)
		newTree.Position = floor
		local treeTiltValue: number = tree:GetAttribute("Tilt")
		local rx, ry, rz: number = math.random(-treeTiltValue,treeTiltValue), math.random(-treeTiltValue,treeTiltValue), math.random(-treeTiltValue,treeTiltValue)
		newTree.Rotation = Vector3.new(rx, ry, rz)
	else
		error("Raycast failed!")
	end

Thanks! I should try looking at those areas more.

1 Like

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