So I made this gun script. It works, but only when I point my mouse to a surface and shoot. I want it to shoot when it points to the sky also.
The script:
01 | tool.Activated:Connect( function () |
02 | firing = true |
03 | while firing = = true do |
04 | local ray = Ray.new(script.Parent.barrelpos 1. Position, (mouse.Hit.p - script.Parent.barrelpos 1. Position).Unit * 100 ) |
05 | local raypart, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, { char } , false , true ) |
06 |
07 | local Dist = (mouse.Hit.p - script.Parent.barrelpos 1. Position).magnitude |
08 |
09 | local Lazer = Instance.new( "Part" ) |
10 | Lazer.Parent = game.Workspace |
11 | Lazer.Anchored = true |
12 | Lazer.CanCollide = false |
13 | Lazer.Size = Vector 3. new( 0.1 , 0.1 , Dist) |
14 | Lazer.CFrame = CFrame.new(script.Parent.barrelpos 1. Position,mouse.Hit.p + Vector 3. new(math.random( 1 , 3 ),math.random( 1 , 3 ),math.random( 1 , 3 ))) * CFrame.new( 0 , 0 , -Dist/ 2 ) |
15 | debris:AddItem(Lazer, 0.1 ) |
16 | wait( 0.3 ) |
17 | end |
18 | end ) |
Any help would be appreciated.
Try replacing mouse.Hit.p
in line 07 with position from line 05. Because when the mouse is pointing towards the sky, there is no mouse.Hit, but there may very well still be a position since the ray will always have an end, even if there is no target.
01 | tool.Activated:Connect( function () |
02 | firing = true |
03 | while firing = = true do |
04 | local ray = Ray.new(script.Parent.barrelpos 1. Position, (mouse.Hit.p - script.Parent.barrelpos 1. Position).Unit * 100 ) |
05 | local raypart, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, { char } , false , true ) |
06 |
07 | local Dist = (position - script.Parent.barrelpos 1. Position).magnitude |
08 |
09 | local Lazer = Instance.new( "Part" ) |
10 | Lazer.Parent = game.Workspace |
11 | Lazer.Anchored = true |
12 | Lazer.CanCollide = false |
13 | Lazer.Size = Vector 3. new( 0.1 , 0.1 , Dist) |
14 | Lazer.CFrame = CFrame.new(script.Parent.barrelpos 1. Position,mouse.Hit.p + Vector 3. new(math.random( 1 , 3 ),math.random( 1 , 3 ),math.random( 1 , 3 ))) * CFrame.new( 0 , 0 , -Dist/ 2 ) |
15 | debris:AddItem(Lazer, 0.1 ) |
16 | wait( 0.3 ) |
17 | end |
18 | end ) |