Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make my gun raycast through non-collided parts?

local head = game.Workspace[player.Name].Head.CFrame.lookVector
		local mouse = CFrame.new(game.Workspace[player.Name].Head.Position,mouse.Hit.p).lookVector
		local ray = Ray.new(tool.Glock.FirePoint.CFrame.p,(player:GetMouse().Hit.p - tool.Glock.FirePoint.CFrame.p).unit*250)
		local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
		local Camera = workspace.CurrentCamera

My gun has the ability to shoot through windows and it uncollides the windows to "break" them. After they are broken, they need to be able to shoot through windows or uncollided parts.

1 comment

0

FindPartOnRay is deprecated, don't use it in new work.

1 answer

✓ Accepted answer

So I don't know how you managed to even find some of this stuff because deprecated things are hidden on the docs, so I'm just going to rewrite this for you using up-to-date methods, and then answer your question as well.

But first I need to get you to understand the purpose of variables in programming. They are not there to look at or keep a reference for yourself, they're there so you can use them in your script. You're only hurting yourself in the long run if you don't use them, and also doing things like setting the mouse to a variable and then getting the mouse all over again for no reason is slowing your script down.

So for an example, when you created the "head" variable you should have set it to just the head, and used it in the next line like so:

local head = game.Workspace[player.Name].Head
local mouse = CFrame.new(head.Position,mouse.Hit.p).lookVector

Second, stop using such a roundabout way of accessing the character like searching the workspace for a character when you already have the Player as a variable. game.Workspace[player.Name] is wrong, use player.Character instead.

Now moving on to your question, using the notes above, and the removal of the deprecated functions you have used, it should look like this:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local FirePoint = tool.Glock.FirePoint--idk what tool is, you didn't actually set it to anything
repeat wait() until Player:HasAppearanceLoaded()--make sure the character is loaded before trying to get any children

local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
RayParams.FilterDescendantsInstances = Player.Character

local Direction = (FirePoint.CFrame.Position-Mouse.Hit.p).Unit--do not make a CFrame just to get it's look vector
local Origin = FirePoint.CFrame.Position

local Result = workspace:Raycast(Origin,Direction*250,RayParams)
local Part, Position = Result.Instance, Result.Position

So that does exactly what you had before with all the redundant variables and deprecated functions removed. Now to get it to ignore CanCollided parts, you can either add them to the Filter or you can check during the raycasting like this:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local FirePoint = tool.Glock.FirePoint--idk what tool is, you didn't actually set it to anything
repeat wait() until Player:HasAppearanceLoaded()--make sure the character is loaded before trying to get any children

local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
RayParams.FilterDescendantsInstances = Player.Character:GetDescendants()

local Direction = (FirePoint.CFrame.Position-Mouse.Hit.p).Unit--do not make a CFrame just to get it's look vector
local Origin = FirePoint.CFrame.Position

local Result = workspace:Raycast(Origin,Direction*250,RayParams)
local Part, Position = Result.Instance, Result.Position
local DistanceToGo = 250-(Position - Origin).Magnitude
if not Part.CanCollide then
	Result = workspace:Raycast(Origin,Direction*DistanceToGo,RayParams)
	Part, Position = Result.Instance, Result.Position
end

It will shoot a ray out, and if it hits a part that is not CanCollided then it will shoot another ray in the same direction for the remaining distance of the original ray.

Since this answers your question, remember to mark it as the accepted answer.

1 comment

0

Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.