Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is Ray in ROBLOX, I've seen it a lot in scripts but I never knew what that means?

Asked by 5 years ago

So, I've seen in some of the scripts using functions like :FindPartOnRay() but I've never knew what does that mean.

Then, I've had enough. Can someone who knows and who really has time to say or teach me what does that mean.

THANKS IN ADVANCE

0
A little Google can do a lot: http://wiki.roblox.com/index.php?title=Ray ee0w 458 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Honestly, the best place to go is Roblox Wiki's page on rays, but to summarise in the most basic terms: a ray is just a line that extends 'infintely' in one direction.

A ray is constructed using just an origin position and a direction:

local ray = Ray.new(Vector3 origin, Vector3 direction)

FindPartOnRay is a very helpful function of Workspace that returns a tuple consisting of the first part the ray intersects and the position at which it intersects. Here's the Wiki page for more information on FindPartOnRay.

Ad
Log in to vote
0
Answered by 5 years ago

Hello there, I hope my short answer will help you out with understanding what :FindPartOnRay() is. That function is used when raycasting. Often people use it when scripting a gun or checking for empty spaces as far as I know. I will provide an example and explain how it works:

local function findEmptyDirection(character) -- I want to find an empty direction in which to teleport a player next to another player so that nobody gets stuck in a wall for instance

    local hRPart = character:FindFirstChild("HumanoidRootPart")

    if hRPart then

        local xYOrZ
        local offset

        for i = 1, 5 do

            if i == 1 then -- various offsets I created and the direction of the ray
                xYOrZ = Vector3.new(0, 10, 0)
                offset = Vector3.new(0, 7, 0)
            elseif i == 2 then
                xYOrZ = Vector3.new(8, 0, 0)
                offset = Vector3.new(5, 0, 0)
            elseif i == 3 then
                xYOrZ = Vector3.new(-8, 0, 0)
                offset = Vector3.new(-5, 0, 0)
            elseif i == 4 then
                xYOrZ = Vector3.new(0, 0, 8)
                offset = Vector3.new(0, 0, 5)
            elseif i == 5 then
                xYOrZ = Vector3.new(0, 0, -8)
                offset = Vector3.new(0, 0, -5)
            end

            local ray = Ray.new(hRPart.Position, xYOrZ) -- here I create the ray for checking for an empty area it starts at hRPart.Position and heads in the direction of xYOrZ 
            local part, hitPosition = workspace:FindPartOnRay(ray, character) --and here is the function you were asking about. All it does is it takes the ray that you created as the first argument as well as anything you want it to ignore as the second argument. 

            if not part then

                return hRPart.Position + offset

            else

                print(part.Name)

            end

        end

        return false

    end

end

In the end if any one of the directions is clear after checking with FindPartOnRay I will teleport the player to that side of the other player. I hope this helps and have a great day scripting!

0
This script may be a bit out of date or badly written idk. I just know the :FindPartOnRay element of it is correct. User#21908 42 — 5y
Log in to vote
0
Answered by
noammao 294 Moderation Voter
5 years ago

Usually, in Roblox people use the ray function in guns. In fortnite for example whenever you shoot at an enemy the game creates a ray ( a line that goes in an infinite direction) from your gun all the way to the enemy. The :FindPartOnRay() means that you have created a ray and you want to see if it has detected any object. For example a player.

Answer this question