Gun not damaging npc or player if too close at npc or player

here’s a video of it happening

LOCAL SCRIPT

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local ReloadAnim
local ReloadTrack = nil
local tool = script.Parent
local handle = tool:FindFirstChild(“Handle”)
local larrel = handle and handle:FindFirstChild(“larrel”)
local debounce = false

local ammo = 34
local maxAmmo = 34
local reloading = false

local function reload ()
end

– Shooting
tool.Activated:Connect(function()
if debounce == false and ammo > 0 and reloading == false then
debounce = true
ammo -= 1
local direction = (mouse.Hit.Position - tool.Handle.Barrel.Position).Unit
tool.Shoot:FireServer(direction)
script.Parent.Handle.Barrel[“Gun shot”]:Play()
workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(2),0,0)
player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Text = “”…ammo…“/”…maxAmmo
player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 0
task.wait(0.1)
debounce = false
elseif ammo <= 0 and reloading == false then
reload()
end
end)

SERVERSCRIPT

local tool = script.Parent
local rep = game.ReplicatedStorage
local bulletline = rep.bullet
local bulletlinecloe = bulletline:Clone()
local animation = script.headhot

script.Parent.Shoot.OnServerEvent:Connect(function(player, direction)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

	local raycastResult = workspace:Raycast(tool.Handle.Barrel.Position, direction * 42, raycastParams)
	
while raycastResult and raycastResult.Instance:IsA("Accessory") do
	raycastResult = workspace:Raycast(raycastResult.Position, (direction - raycastResult.Position).Unit * 42, raycastParams)
end

if raycastResult then
	local raycastInstance = raycastResult.Instance
	local model = raycastInstance:FindFirstAncestorOfClass("Model")
	local wood = raycastInstance.Material == Enum.Material.Wood
	local metal = raycastInstance.Material == Enum.Material.Metal
	local smooth = raycastInstance.Material == Enum.Material.SmoothPlastic
	local humanoid = model and model:FindFirstChildWhichIsA("Humanoid")

	if model and model.Name == "Door" then
	end
	
	if wood then
end
	
	if metal then
end
	
	if smooth then
	end
	
	if model and model.Name == "glass" then
	end			
		
		if humanoid then
			if raycastInstance.Name == "Head" then
				local anmier = model:FindFirstChild("Humanoid"):WaitForChild("Animator")
				local play = anmier:LoadAnimation(animation)
				humanoid:TakeDamage(43)
				play:Play()
				script.Parent.Handle.hit:Play()
				local ind = Instance.new("Part")
				ind.Parent = workspace
				ind.CFrame = CFrame.new(raycastResult.Position)
				ind.Size = Vector3.new(1.158, 0.45, 0.979)
				ind.Material = Enum.Material.Ground
				ind.BrickColor = BrickColor.new("Nougat")
				ind.CanCollide = false
				ind.Anchored = false
				ind.Transparency =  1
				local partiec = Instance.new("ParticleEmitter", ind)
				partiec.Rate = 100
				partiec.Size = NumberSequence.new(1.0)
				partiec:Emit(100)
				partiec.Texture = "rbxassetid://5069294691"
				partiec.Color = ColorSequence.new(Color3.fromRGB(85, 0, 0))
				partiec.EmissionDirection = "Top"
				partiec.Transparency = NumberSequence.new(0.3)
				partiec.Rotation = NumberRange.new(3,3)
				partiec.RotSpeed = NumberRange.new(2,2)
				partiec.Lifetime = NumberRange.new(0.8,0.8)
				partiec.Speed = NumberRange.new(8, 8)
				partiec.RotSpeed = NumberRange.new(2, 2)
				partiec.SpreadAngle = Vector2.new(100,100)
				task.wait(0.3)
				ind:Destroy()
				partiec:Destroy()
			else
				humanoid:TakeDamage(25)
				script.Parent.Handle.hit:Play()
				local ind = Instance.new("Part")
				ind.Parent = workspace
				ind.CFrame = CFrame.new(raycastResult.Position)
				ind.Size = Vector3.new(1.158, 0.45, 0.979)
				ind.Material = Enum.Material.Ground
				ind.BrickColor = BrickColor.new("Nougat")
				ind.CanCollide = false
				ind.Anchored = false
				ind.Transparency =  1
				local partiec = Instance.new("ParticleEmitter", ind)
				partiec.Rate = 100
				partiec.Size = NumberSequence.new(1.0)
				partiec:Emit(100)
				partiec.Texture = "rbxassetid://5069294691"
				partiec.Lifetime = NumberRange.new(0.8,0.8)
				partiec.Color = ColorSequence.new(Color3.fromRGB(85, 0, 0))
			partiec.EmissionDirection = "Top"
			partiec.Transparency = NumberSequence.new(0.3)
			partiec.Rotation = NumberRange.new(3,3)
			partiec.RotSpeed = NumberRange.new(2,2)
			partiec.Speed = NumberRange.new(8, 8)
			partiec.RotSpeed = NumberRange.new(2, 2)
			partiec.SpreadAngle = Vector2.new(100,100)
			task.wait(0.3)
			ind:Destroy()
			partiec:Destroy()
		end
	end
end

end)

4 Likes

It’s probably because the barrel goes inside the NPC.

Raycasts won’t detect objects it’s inside in, so I recommend using an attachment instead of the actual gun’s barrel as your raycast’s origin, and sort of move it at the back of the gun rather than the front.

Keep pushing the attachment further back until it works well enough.

2 Likes