I am trying to make a gun system in which once a player is shot, they will die. The problem is, when accessories are equipped, they act as a shield, and I do not want this to occur. I have been trying to find a way to solve this though I do not know how.
Local Script:
local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local replicatedStorage = game:GetService("ReplicatedStorage")
local shootEvent = replicatedStorage:WaitForChild("ShootEvent")
local DAMAGE_AMOUNT = 100
local BULLET_HOLE_DECAL = "http://www.roblox.com/asset/?id=1844445084"
local BULLET_HOLE_SIZE = Vector3.new(1, 1, 0.001)
local OFFSET_DISTANCE = 0.001
local MAX_PROXIMITY = 0.1
local bulletHolePositions = {}
local debounce = false
local function bulletHoleExists(position)
for _, pos in ipairs(bulletHolePositions) do
local distance = (pos - position).Magnitude
if distance <= MAX_PROXIMITY then
return true
end
end
return false
end
local function createBulletHole(position, normal)
local adjustedPosition = position + normal * OFFSET_DISTANCE
if bulletHoleExists(adjustedPosition) then
return
end
local bulletHole = Instance.new("Part")
bulletHole.Size = BULLET_HOLE_SIZE
bulletHole.Anchored = true
bulletHole.CanCollide = false
bulletHole.CFrame = CFrame.new(adjustedPosition, adjustedPosition + normal)
bulletHole.Transparency = 1
bulletHole.Name = "BulletHole"
local decal = Instance.new("Decal")
decal.Texture = BULLET_HOLE_DECAL
decal.Face = Enum.NormalId.Front
decal.Parent = bulletHole
bulletHole.Parent = workspace
table.insert(bulletHolePositions, adjustedPosition)
game.Debris:AddItem(bulletHole, 10)
delay(10, function()
for i, pos in ipairs(bulletHolePositions) do
if pos == adjustedPosition then
table.remove(bulletHolePositions, i)
break
end
end
end)
end
local function shoot()
if debounce then return end
debounce = true
if not player.Character or not player.Character:FindFirstChild("Head") then
debounce = false
return
end
local fireSound = tool:FindFirstChild("fire")
if fireSound then fireSound:Play() end
local origin = player.Character.Head.Position
local direction = (mouse.Hit.p - origin).unit * 1000
local rayParams = RaycastParams.new()
rayParams.IgnoreWater = true
rayParams.FilterType = Enum.RaycastFilterType.Exclude
-- Exclude accessories directly by adding them to the filter table
local filterTable = {player.Character}
for _, accessory in ipairs(player.Character:GetChildren()) do
if accessory:IsA("Accessory") then
table.insert(filterTable, accessory)
end
end
rayParams.FilterDescendantsInstances = filterTable
local raycastResult = workspace:Raycast(origin, direction, rayParams)
if raycastResult then
local hit = raycastResult.Instance
local position = raycastResult.Position
local normal = raycastResult.Normal
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
shootEvent:FireServer(hit.Parent, DAMAGE_AMOUNT)
else
if hit.Parent and not hit.Parent:FindFirstChildWhichIsA("Accessory") then
createBulletHole(position, normal)
end
end
end
task.delay(1.5, function() debounce = false end)
end
tool.Activated:Connect(shoot)