Edit: The answer to the OP’s original question is at the bottom of this reply.
workspace:Raycast() works in studio now, so you should use that instead.
Here’s an example on how you can use it:
local UserInputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local GUI_INSET = Vector2.new(0, 36)
local RANGE = 1e3 -- 10^3
local RAYCAST_PARAMS = RaycastParams.new()
RAYCAST_PARAMS.IgnoreWater = false
RAYCAST_PARAMS.FilterDescendantsInstances = {Player.Character or Player.CharacterAdded:Wait()}
RAYCAST_PARAMS.FilterType = Enum.RaycastFilterType.Blacklist
local function get3DPosition()
local screenPosition = UserInputService:GetMouseLocation() - GUI_INSET
local oray = Camera:ScreenPointToRay(screenPosition.X, screenPosition.Y, 0)
return workspace:Raycast(oray.Origin, oray.Direction * RANGE, RAYCAST_PARAMS)
end
Player.CharacterAdded:Connect(function(character)
RAYCAST_PARAMS.FilterDescendantsInstances[1] = character
end)
-- how you get each item:
local raycastResult = get3DPosition()
if raycastResult then
-- it's nil if it doesn't hit a part
local part = raycastResult.Instance
local pos = raycastResult.Position
local normal = raycastResult.Normal
end
Source: (run this in the command bar)
print(not not workspace.Raycast)
This prints true if the function exists, and false if it doesn’t. You can use the same method to check other functions as well. (the not not
has a functional purpose.)
Firstly, .p
is deprecated so you should use .Position
instead.
Now, to actually solve your raycasting issue. The reason it didn’t work is because the second parameter of Ray.new() is the direction, not the end position.
To create a Vector3 direction with the end position, you have to do this:
local startPosition = script.Parent["173Mesh"].CFrame.Position
local endPosition = target.CFrame.Position
local ray = Ray.new(startPosition, (endPosition - startPosition))
The (endPosition - startPosition)
is what the direction portion (the second parameter of) your Ray.new() should be.
Again, I recommend using workspace:Raycast()
though.