What is the difference between workspace: Ray.new () and workspace:Raycast

As i told, the title says it all.

3 Likes

There is no “workspace:Ray.new” Ray.new was the deprecated function to create raycasts, it does just the same as workspace:Raycast, but it’s old and it’s not meant to be used anymore.

4 Likes

So, what i should use?, is one of them better?, or alike?, I mean, in his complex, easy to understand

1 Like

U should always use workspace:Raycast, because Ray.new is deprecated, take a look on the forum → WorldRoot | Roblox Creator Documentation

2 Likes

Ok, got it now, but why people still Uses Ray.new() ?.

You probably saw some old video, but people shouldn’t be using it at all

If you could show me an example of how to use the workspace: Raycast, unfortunately I am on mobile and there is no concrete example in the guide

False. I don’t see where it is deprecated. Please give us another reliable resource to prove it.

4 Likes

This is where i saw it’s deprecated, this method uses the Ray.new function:
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartOnRay

This guy helped me a lot when i started, you can probably have good use of his channel:

1 Like

Still false, that function is equivalent to workspace:Raycast() with RaycastParams in it, it just says that function is deprecated, not the Ray.new(). So the Ray function is NOT deprecated.

4 Likes

Hmm, i see, well, even if it’s not deprecated, there are new methods to do it, and that’s because they’re probably better then the old ones, so it’s still better to use them, thats what i think at least. Thanks for correcting me!

It’s pretty much only legacy code that uses Ray.new, and people who have old habits to use it.

The Ray.new method does have one small advantage in that you don’t have to make a RaycastParams object to specify a blacklist/whitelist, but it doesn’t have much else going for it beyond that. I believe it is also not going to get any new features like collision group filtering.

The WorldRoot:Raycast method has the advantage of consolidating all of its options into one RaycastParams parameter, where you can make a new RaycastParams object, fill in only the settings that you need, and can cache those settings between ray casts.

I’m pretty sure they both link to the same code path internally, it’s just that the Ray.new method would become exponentially harder to extend, because they opted to make new functions for every permutation of configurations.

The WorldRoot:Raycast method is pretty simple, it’s just workspace:Raycast(start, direction) or workspace:Raycast(start, direction, raycastParams) where raycastParams is an object created with RaycastParams.new(). You fill in the members you need in raycastParams like setting the collision group, or filter & filter mode, then pass it in to the Raycast function.

2 Likes

Thanks for all the explanations, but I can use workspace:Raycast() on a localScript?, or only server-side?

Both sides are allowed to access this function.

1 Like

This is correct.

Always opt for WorldRoot:Raycast() because Ray.new() will see little to no functionality updates moving forward. I imagine it will eventually be deprecated entirely. Both will perform at essentially the same cost.

Yeah, you should be able to. It doesn’t change anything in the scene, it just queries the scene asking for information about it. That’s a network safe feature to allow clients to use. Changes that are derived from the results may not be network safe, but that doesn’t mean the function itself isn’t network safe.

1 Like

Do you think this script would work? I made it on reference from the Guide, and is on a LocalScript


local NPCModel = script.parent

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.FilterDescendantsInstances = {NPCModel}
raycastParams.IgnoreWater = true
 
-- Cast the ray
local part = workspace.Part
local raycastResult = workspace:Raycast(part.Position, part.CFrame.LookVector*50, raycastParams)
 
-- Interpret the result
if raycastResult then
	for i,v in pairs(game:GetService("Players"):GetPlayers()) do
        print("Got Player")

	print("Hit position:", raycastResult.Position)

       end
else
	print("Nothing was hit!")
end

Forget about that, It worked very perfect.

With FindPartOnRay deprecated there is next to no real use for Ray.new, the only reason it isn’t fully deprecated I’d assume is to give developers a grace period to switch over to the new method, so unless your down with having to go through and change your code in the near future don’t use Ray.new.