Why can't I change the length of a ray?

Hello everyone, so when I do this:

local ray = workspace:Raycast(gun.Position, gun.CFrame.LookVector * 500)

Where “gun” is a simple union that is in the shape of a gun held by the player, when I visualize the ray it shows that the length was not changed at all and it is at default (1) even though I clearly expanded the direction of the raycast to “gun.CFrame.LookVector * 500”. Does anyone know what is going on or how I can change the length of a ray to make it longer? Thanks a lot!

If there are parts blocking the way then does the ray cut short? Sorry, this is really one of the first time i’ve used rays. Thanks for the answer.

The ray stops immediately when it runs into a part or winds up at its destination, you don’t have an ignorelist included so it’s probably running into something

I could be wrong obviously, but yes it gets cut short when it runs into something

1 Like

I have:

rayParam.FilterDescendantsInstances = {workspace.ThePart} rayParam.FilterType = Enum.RaycastFilterType.Exclude

But it still says it collided with “ThePart”. What did I do wrong? Thank you.

Are you actually putting that filter list into the raycast?

local ray = workspace:Raycast(gun.Position, gun.CFrame.LookVector * 500, rayParam)

Yeah I did that now but all i’m getting is an error:

Players.crossbar.PlayerGui.MouseHandler:162: attempt to index nil with 'Position'

Thanks for answering.

It sounds like the issue may be related to how you are visualizing the ray. The length of the raycast itself should be determined by the distance between the start and end points of the ray, which in your case is defined by the gun’s position and its LookVector multiplied by 500.

To visualize the ray, you can create a new part or line between the start and end points of the ray. Here’s an example of how you could do this in Roblox Lua:

-- Define the start and end points of the raycast
local rayStart = gun.Position
local rayEnd = gun.Position + (gun.CFrame.LookVector * 500)

-- Perform the raycast
local raycastResult = workspace:Raycast(rayStart, rayEnd)

-- Create a new part to visualize the ray
local rayPart = Instance.new("Part")
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.Size = Vector3.new(0.2, 0.2, (rayStart - rayEnd).Magnitude)
rayPart.CFrame = CFrame.new(rayStart, rayEnd) * CFrame.new(0, 0, -rayPart.Size.Z/2)
rayPart.Parent = workspace

-- Optional: change the color of the part to indicate whether the ray hit something
if raycastResult then
    rayPart.Color = Color3.new(0, 1, 0) -- Green if the ray hit something
else
    rayPart.Color = Color3.new(1, 0, 0) -- Red if the ray did not hit anything
end

This code creates a new part that spans the length of the raycast, and positions it between the start and end points of the ray. It also changes the color of the part based on whether the ray hit something or not, which you can remove if you don’t need it.

Note that the size of the part is calculated as the distance between the start and end points of the ray using the Magnitude property of a vector. This is what ensures that the length of the ray is correctly represented by the size of the part.

note: this was written by chatgpt and is 99.9% likely to be false

2 Likes

What’s on that line? Is it the same line as you already posted?

I think its caused because I put actual instances into rayParam.FilterDescendantsInstances rather than a folder containing the instances. Am I right? Thanks again.

It can be either an instance or a folder containing instances, it doesn’t matter

A folder is an instance anyway

Okay but when I put anything for the rayParam.FilterDescendantsInstances = part it just says ray is nil. What’s going on with that? Thanks.

Ray is always nil if it hasn’t collided with an object iirc (if it hits an object in its exclude list, it keeps going)

I see. But when I make “ThePart” in the Exclude list, the parts that are obviously behind it that my gun shoots through don’t make it to the detection list for the ray. Do you know why? I thought that was the point of rays… Thanks for all the help so far too. In fact when I visualize the ray I don’t see anything at all.

I’m not having any issues with raycasting, it’s possible that you need to add the lookvector to the position
Demo game showing a part shooting a ray towards a wall: (this uses an includelist instead of an excludelist, but you get the point)

Baseplate.rbxl (37.4 KB)

Thanks. I’ll see what I can do, do some testing myself with raycasting, and post here if I still can’t find a solution. Thanks for helping me this far as well. I appreciate it a lot. In the end, I think I might just unanchor it and use Touched() event since it seems to work fine for things being deleted quickly like bullets.

If you’re making a PVP game, this is a bad idea. Exploiters can prevent Touched signals from firing by just deleting the object

It will be done through ServerEvents through the server, so mouse in LocalScript that gets the Touched() event sends to server to deduct damage from whatever. I think this will work but let me know if it doesn’t (obviously). Thanks!

It’s also an issue in serverscripts, touched events aren’t sent to the server if the client doesn’t know it touched a part.

Well truthfully, if exploiters have access to the server and can delete things then I have more to worry about (like them deleting the entire map!) but since Roblox tells me ServerEvents and RemoteFunctions are a good way to deter that I guess I will just try to incorporate that into this code(?). I think if I do it this way it’ll work because you can’t really manipulate Touched() events after the client sends it up to the server that does the work then. Thanks for helping me out, really appreciate it!