Hello. I was working on my project then I saw that bullet goes through walls!
My gun is like this:
a bullet that tweens to the mouse.hit
I tried to use region3 to check what’s inside the bullet with a ignore list
The local script in the tool:
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local CanShoot = false
local CanKeepShoot = true
local CanReload = true
local Tool = script.Parent
local AmmoPack = 5
local Ammo = 10
local UIS = game:GetService(“UserInputService”)
local function Reload()
if AmmoPack >= 0 then
if CanReload == true then
local animtype = "Reload"
script.Parent.PlayAnim:FireServer(animtype)
CanReload = false
CanKeepShoot = false
wait(1.5)
Ammo = 10
AmmoPack = AmmoPack - 1
CanReload = true
CanKeepShoot = true
end
end
end
UIS.InputBegan:Connect(function(keycode, ischatting)
if ischatting == false then
if keycode.KeyCode == Enum.KeyCode.R then
Reload()
end
end
end)
Tool.Equipped:Connect(function()
CanShoot = true
end)
Tool.Unequipped:Connect(function()
CanShoot = false
end)
Mouse.Button1Up:Connect(function()
if CanShoot == true then
if CanKeepShoot == true then
if Ammo >= 0 then
Ammo = Ammo - 1
local cframe = Mouse.Hit
script.Parent.Shoot:FireServer(cframe)
local animtype = "Shoot"
script.Parent.PlayAnim:FireServer(animtype)
CanKeepShoot = false
wait(0.5)
CanKeepShoot = true
else
Reload()
end
end
end
end)
a script inside the tool that handles remote events:
local TweenService = game:GetService(“TweenService”)
local Tool = script.Parent
local Info = TweenInfo.new(
0.1,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
false,
0
)
script.Parent.Shoot.OnServerEvent:Connect(function(player, cframe)
local Bullet = game.ReplicatedStorage.Bullet:Clone()
Bullet.Parent = game.Workspace
Bullet.CFrame = Tool.Handle.CFrame
--Bullet.CFrame = cframe
--Bullet.Position = Tool.Handle.Position
--Bullet.CFrame = Bullet.CFrame + Bullet.CFrame.LookVector * 2
Bullet.Owner.Value = player.Name
local newcframe = cframe + cframe.LookVector * 10
local Tween = TweenService:Create(Bullet, Info, {CFrame = cframe})
Tween:Play()
Tween.Completed:Connect(function()
Bullet:Destroy()
end)
end)
And the script inside the bullet:
local region = Region3.new(script.Parent.RegionPart1.Position, script.Parent.RegionPart2.Position)
local player = game.Workspace:FindFirstChild(script.Parent.Owner.Value)
local okparts = {
script.Parent,
script.Parent.RegionPart1,
script.Parent.RegionPart2,
}
if player then
for i,v in ipairs(player:GetChildren()) do
table.insert(okparts, v)
end
end
local partsinregion = game.Workspace:FindPartsInRegion3WithIgnoreList(region, okparts, 100)
while true do
wait(0)
region = Region3.new(script.Parent.RegionPart1.Position, script.Parent.RegionPart2.Position)
local partsinregion = game.Workspace:FindPartsInRegion3WithIgnoreList(region, okparts, 100)
if #partsinregion >= 1 then
for i,v in ipairs(partsinregion) do
if v.Parent:FindFirstChild("Humanoid") or v.Parent.Parent:FindFirstChild("Humanoid") then
local humanoid = v.Parent:FindFirstChild("Humanoid") or v.Parent.Parent:FindFirstChild("Humanoid")
humanoid:TakeDamage(15)
if humanoid.Health <= 0 then
local playernotcharacter = game.Players:FindFirstChild(script.Parent.Owner.Value)
playernotcharacter.leaderstats.Cash.Value = playernotcharacter.leaderstats.Cash.Value + 60
end
end
print(v.Name)
end
script.Parent:Destroy()
end
wait(0)
end
Now, what’s the problem?
The bullet goes through walls and the player can damage itself
Now how do I prevent these from happening?