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

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

Asked by
H34E7R 19
4 years ago
1local head = game.Workspace[player.Name].Head.CFrame.lookVector
2        local mouse = CFrame.new(game.Workspace[player.Name].Head.Position,mouse.Hit.p).lookVector
3        local ray = Ray.new(tool.Glock.FirePoint.CFrame.p,(player:GetMouse().Hit.p - tool.Glock.FirePoint.CFrame.p).unit*250)
4        local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
5        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.

0
FindPartOnRay is deprecated, don't use it in new work. SteamG00B 1633 — 4y

1 answer

Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
4 years ago
Edited 4 years ago

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:

1local head = game.Workspace[player.Name].Head
2local 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:

01local Player = game.Players.LocalPlayer
02local Mouse = Player:GetMouse()
03local FirePoint = tool.Glock.FirePoint--idk what tool is, you didn't actually set it to anything
04repeat wait() until Player:HasAppearanceLoaded()--make sure the character is loaded before trying to get any children
05 
06local RayParams = RaycastParams.new()
07RayParams.FilterType = Enum.RaycastFilterType.Blacklist
08RayParams.FilterDescendantsInstances = Player.Character
09 
10local Direction = (FirePoint.CFrame.Position-Mouse.Hit.p).Unit--do not make a CFrame just to get it's look vector
11local Origin = FirePoint.CFrame.Position
12 
13local Result = workspace:Raycast(Origin,Direction*250,RayParams)
14local 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:

01local Player = game.Players.LocalPlayer
02local Mouse = Player:GetMouse()
03local FirePoint = tool.Glock.FirePoint--idk what tool is, you didn't actually set it to anything
04repeat wait() until Player:HasAppearanceLoaded()--make sure the character is loaded before trying to get any children
05 
06local RayParams = RaycastParams.new()
07RayParams.FilterType = Enum.RaycastFilterType.Blacklist
08RayParams.FilterDescendantsInstances = Player.Character:GetDescendants()
09 
10local Direction = (FirePoint.CFrame.Position-Mouse.Hit.p).Unit--do not make a CFrame just to get it's look vector
11local Origin = FirePoint.CFrame.Position
12 
13local Result = workspace:Raycast(Origin,Direction*250,RayParams)
14local Part, Position = Result.Instance, Result.Position
15local DistanceToGo = 250-(Position - Origin).Magnitude
16if not Part.CanCollide then
17    Result = workspace:Raycast(Origin,Direction*DistanceToGo,RayParams)
18    Part, Position = Result.Instance, Result.Position
19end

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.

0
Thank you so much! H34E7R 19 — 4y
Ad

Answer this question