Unable to cast Instance to Vector3

I’m trying to make a laser using raycast, and I don’t know what I’m doing wrong.

function laser()
	if tool:FindFirstChild("Laser") then
		local laserorigin = tool.Laser.Main.StartAttachment
		local laserparams = RaycastParams.new()
		laserparams.FilterDescendantsInstances = {tool.Laser.Main}
		laserparams.FilterType = Enum.RaycastFilterType.Blacklist
		local rayresult = workspace:Raycast(laserorigin, tool.Laser.Main.StartAttachment.Orientation, laserparams)
		local hit = rayresult.Instance
		tool.Laser.Main.EndAttachment.Position = Vector3.new(0, (hit.Position.Y-tool.Laser.Main.Position.Y)+rayresult.Position.Y, 0)
	end
end
2 Likes
function laser()
	if tool:FindFirstChild("Laser") then
		local laserorigin = tool.Laser.Main.StartAttachment
		local laserparams = RaycastParams.new()
		laserparams.FilterDescendantsInstances = {tool.Laser.Main}
		laserparams.FilterType = Enum.RaycastFilterType.Blacklist
		local rayresult = workspace:Raycast(laserorigin.Position, tool.Laser.Main.StartAttachment.Orientation, laserparams)
		local hit = rayresult.Instance
		tool.Laser.Main.EndAttachment.Position = Vector3.new(0, (hit.Position.Y-tool.Laser.Main.Position.Y)+rayresult.Position.Y, 0)
	end
end

You’re feeding an instance into a function that expects a Vector3. Also, you’re assuming that the raycast hits something.

1 Like

Forgive me, but I don’t really know much about raycasts, and don’t really know what you’re talking about.

1 Like

You’ve given us very little information to work with. Please show us the error and variables!

laser orgin is an attachment not a position

function laser()
	if tool:FindFirstChild("Laser") then
		local laserorigin = tool.Laser.Main.StartAttachment
		local laserparams = RaycastParams.new()
		laserparams.FilterDescendantsInstances = {tool.Laser.Main}
		laserparams.FilterType = Enum.RaycastFilterType.Blacklist
		local rayresult = workspace:Raycast(laserorigin.WorldPosition, tool.Laser.Main.StartAttachment.Orientation, laserparams)
		local hit = rayresult.Instance
		tool.Laser.Main.EndAttachment.Position = Vector3.new(0, (hit.Position.Y-tool.Laser.Main.Position.Y)+rayresult.Position.Y, 0)
	end
end
2 Likes