Help with Gun tool?

Hello!

I am making a game for practice and I have learned ray casting and a bit more!
The weapon works fine, the only problem I’m having is when a player shoots another, the bullet hits the player and sometimes the player doesn’t die, here is a screenshot above:

I have tried looking back through the code and found nothing, aswell as the output not saying anything.

Here is the codes to the gun:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local mouse = game.Players.LocalPlayer:GetMouse()
local Tool = script.Parent

script.Parent.Equipped:Connect(function(mouse)
mouse.Icon = “http://www.roblox.com/asset/?id=79658449
end)

local Cooldown = false
local COOLDOWN_TIME = 3

local function Reload()
script.Parent.Handle.Reloading:Play()
end

local function Pew()
game.ReplicatedStorage.ShootGun:FireServer(Mouse.hit.p, Tool.Handle)
end

local function Shoot()
– Function code.

if Cooldown == true then
return
end

Cooldown = true
Pew()

wait(1)
Reload()

print(“Shoots!”)

wait(COOLDOWN_TIME)
Cooldown = false

end

Tool.Activated:Connect(function()
– This will run whenever someone clicks with the tool in their hand.

Shoot()
end)

Here is the second:

local RANGE = 500
local DAMAGE = 100

game.ReplicatedStorage.ShootGun.OnServerEvent:Connect(function(Player, TargetLocation, Handle)
if Player.Character == nil then – Make sure their character exists.
return
end

Handle.ShootingSound:Play()

local Beam = Instance.new(“Part”, workspace)
Beam.BrickColor = BrickColor.new(“Fossil”)
Beam.FormFactor = “Custom”
Beam.Transparency = 0.55
Beam.CastShadow = false
Beam.Anchored = true
Beam.CanCollide = false

– Math.
local Distance = (Handle.Position - TargetLocation).magnitude
Beam.Size = Vector3.new(0.1, 0.1, Distance)
Beam.CFrame = CFrame.new(Handle.Position, TargetLocation) * CFrame.new(0, 0, -Distance/2)

game.Debris:AddItem(Beam, 0.1) – Destroy.

–RayCasting [Shoot a ray, get wherever it hit]
local NewRay = RaycastParams.new()
local RayDirection = (TargetLocation - Handle.Position) * RANGE – How far you want the ray to travel before stopping

NewRay.FilterDescendantsInstances = {Player.Character}

local Result = workspace:Raycast(Handle.Position, RayDirection, NewRay)

if Result then – If we got a result back.
if Result.Instance then
– It actually hit something.

  	if Result.Instance.Parent:FindFirstChild("Humanoid") then
  		Result.Instance.Parent.Humanoid.Health -= DAMAGE
  		Handle.HitPlayer:Play()
  	end
  end

end
end)

I have no idea wether i missed something for the raycast to properly work or wether this is completly normal!

Please help!

Cheers. :wink:

First of all, I know that it’s a good thing to learn about things, and trying out too.
But sometimes there are better options, and for here, try to learn FastCast.

Anyways, You should change this line to this,
your line: Result.Instance.Parent.Humanoid.Health -= DAMAGE
change to: Result.Instance.Parent.Humanoid:TakeDamage(DAMAGE)

The rest of the code is quite messy, and I can’t really interpret anything.

This is probably because the result sometimes hits accessories. Accessory parts are inside another accessory instance. To avoid this, you can try using

if Result then --Results always have Result.Instance
	local ResultModel = Result.Instance:FindFirstAncestorOfClass("Model")
	local Humanoid = ResultModel and ResultModel:FindFirstChildOfClass("Humanoid")
  	if Humanoid then
  		Humanoid.Health -= DAMAGE -- Also try Humanoid:TakeDamage(DAMAGE)
  		Handle.HitPlayer:Play()
	end
end