I am trying to make glass shatter when hit by a ray, and right now, whenever a ray is created, it checks if the name of the part it hits is “BreakableGlass” and runs a ModuleScript function if yes.
I am wondering if there is a more efficient way of knowing when to shatter the glass, like running the function when the part is hit by a ray.
If you are using RayCasting, then you can do this:
the Workspace has a function called FindPartOnRay which returns 2 values: the first part the ray touches, the position where the ray touched the part.
with the said part, you can check wether the part is what youre looking for.
local ray = --this is the ray you created
local ignoreinstance = --this is the instance that your ray will ignore, note that it will ignore all its descendants too
local part, position = workspace:FindPartOnRay(ray, ignoreinstance, false, true)
if part.Name == "BreakableGlass" then
On the off chance you want the ray to ignore more than one object, you can use FindPartOnRayWithIgnoreList, where on the second argument you must give a table instead of an instance.
Hope this helped!