How to fix my OOP raycasting module?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am trying to make my ray module OOP based. Before I simply had functions without any meta tables but after seeing how powerful OOP can be I am attempting to convert it.

  1. What is the issue? Include screenshots / videos if possible!

I get the error that says attempt to index boolean with printinstancename so I assume the newcar is returning false. I do not know why it is not calling the function as I am using metatables.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I looked over the dev forum post I got it from All about Object Oriented Programming but this is looks exactly like mine.

Here is the current ray module

RayService._index = RayService

function RayService.new(origin, endPos, playerName)
	local newray = {}
	setmetatable(newray, RayService)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {workspace.Baseplate}
	raycastParams.FilterType = Enum.RaycastFilterType.Whitelist

	local result = workspace:Raycast(origin, (endPos - origin), raycastParams)

	if result then
		newray.Instance = result.Instance
		newray.Position = result.Position
		newray.Material = result.Material
	else
		newray = false
	end

	return newray
end

function RayService:PrintInstanceName()
	print(self.Instance.Name)
end

return RayService
1 Like

It should be __index (two underscores).

4 Likes

thank you very much. I should have seen that lol :slight_smile:

2 Likes