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

How to make a gun bullet ignore parts? Like walls and objects.

Hey guys I have a problem. I have a script that shoots players that aren't me and It works perfectly. But the problem is that the bullet shoots through walls and objects. Any idea why and how to make it not do it?

Here's the script:

local Rel = 0
local MaxDis = 100
local Mode = "Normal"

local Lazor = Instance.new("Part")
Lazor.BrickColor = BrickColor.new("really red")
Lazor.Anchored = true
Lazor.BottomSurface = "Smooth"
Lazor.TopSurface = "Smooth"
Lazor.CanCollide = false
Lazor.Transparency = 0.3
Lazor.Reflectance = 0.2
local Mesh = Instance.new("CylinderMesh")
Mesh.Scale = Vector3.new(0.2,1,0.2)
Mesh.Parent = Lazor
Mesh.Offset = Vector3.new(-0.2,0,0)

function CheckHuman(Humanoid, Player)
   if Humanoid.Health == 0 then return false end
	if Player.Name == "wilsonsilva007" then return false end -- We're not gunna shoot at our owner!
   return true
end

function GetDistance(Player, C_Pos)
   local Character = Player.Character
   if Character~=nil and Mode == "Normal" then
      local Head = Character:findFirstChild("Head")
      local Human = Character:findFirstChild("Humanoid")
      if Head~= nil and Human~= nil then
         if CheckHuman(Human, Player) then
            local Distance_1 = Head.Position-C_Pos
            local Distance = Distance_1.magnitude
            return Distance, Head
         end
      end
   end
   return MaxDis*5, nil
end

function GetNearestHead()
   local Pos = script.Parent.Position
   local nearest = nil
   local nearestdis = MaxDis*5
   local NearestHead = nil
   local P = game.Players:GetChildren()
   for i = 1, #P do 
      local Dis, His_Head = GetDistance(P[i], Pos)
      if Dis<= MaxDis*5 and Dis<nearestdis then 
         nearest = P
         nearestdis = Dis
         NearestHead=His_Head
      end
   end
   return NearestHead, nearestdis
end 

function LookAt(Head)
   script.Parent.CFrame = CFrame.new(script.Parent.Position, Head.Position)

   script.Parent.Parent.Main.CFrame = script.Parent.CFrame + script.Parent.CFrame.lookVector*1.6
   script.Parent.Parent.Main.CFrame = script.Parent.Parent.Main.CFrame * CFrame.Angles(math.pi/2,0,0)
   script.Parent.Parent.Shotpart.CFrame = script.Parent.CFrame + script.Parent.CFrame.lookVector*1.6
   script.Parent.Parent.Shotpart.CFrame = script.Parent.Parent.Shotpart.CFrame * CFrame.Angles(math.pi/2,0,0)
   script.Parent.Parent.Scanner.CFrame = script.Parent.CFrame + script.Parent.CFrame.lookVector*1.6
   script.Parent.Parent.Scanner.CFrame = script.Parent.Parent.Scanner.CFrame * CFrame.Angles(-math.pi/2,0,0)
end

function Shoot(Head, Distance, Damage)
   script.Parent.Bang.Volume = 5
   script.Parent.Bang:Play()

   LookAt(Head)
   
   local Hum = Head.Parent:findFirstChild("Humanoid") if Hum ~= nil then Hum.Health = Hum.Health - (--[[DMG]] Hum.MaxHealth*Damage) end

   local Laser = Lazor:clone()
   Laser.Size = Vector3.new(1,Distance,1)
   Laser.CFrame = script.Parent.CFrame + script.Parent.CFrame.lookVector * Distance/2
   Laser.CFrame = Laser.CFrame * CFrame.Angles(math.pi/2,0,0)
   Laser.Parent = workspace
   wait()
   Laser:remove()
   Rel = 1
end

while true do
   local Head, Distance = GetNearestHead()
   if Head~= nil and Rel <= 0 and Distance <= MaxDis then
      Shoot(Head, Distance, 0.05)
   elseif Head ~= nil then
      LookAt(Head)
   end
   if Rel > 0 then Rel = Rel - 1 end
   wait()
end 

4 comments

0

Try setting the CanCollide property of Lazor to true.

0

Laser.CFrame = script.Parent.CFrame + script.Parent.CFrame.lookVector * Distance/2 You do not check for any parts that are in the way.

0

Use RayCasting instead of Part bullets

0

Any Idea how?

1 answer

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