Raycast not detecting player's head and some other areas of the body

For some reason, when raycasting a bullet, the raycast will print nil when shooting at the player’s head or the top of the arm, but it works just fine when shooting at the lower arm or at any other body part.

local raycastresult = workspace:Raycast(script.Parent.BulletPart.Position,(mousePos - script.Parent.Handle.Position)*300000,raycastparams)
	print(mousePos)
	print(raycastresult)
	print(raycastresult)
	if raycastresult then
		local hp = raycastresult.Instance
		print(hp)
		print(hp.Parent)
		print(hp.Parent.Parent)
		local model = hp:FindFirstAncestorOfClass("Model")
		if model or hp.Name == "Left Arm" or hp.Name == "Right Arm" then
			if model:FindFirstChild("Humanoid") or hp.Name == "Left Arm" or hp.Name == "Right Arm" then
				if dmgdb == false then
					if model.Name == script.Parent.Parent.Name then
						print("hit self")
					else
						if hp.Name == "Left Leg" or hp.Name == "Right Leg" then
							script.Parent.DAMAGE:FireClient(player,game.Players:GetPlayerFromCharacter(model),85)
							wait(.1)
							script.Parent.BulletPart.Hit:Play()
							if model.Humanoid.Health ~= 0 and model.Humanoid.Health <= 15 then
								script.Parent.BulletPart.KillSound:Play()
								player.leaderstats.Kills.Value += 1
								game.ReplicatedStorage.KillGui:FireClient(player, model)
								game.ReplicatedStorage.Alive.Value -= 1
							elseif model.Humanoid.Health ~= 0 and model.Humanoid.Health >= 15 then
								model.Humanoid.Health -= 85
							end
							wait(2)
							script.Parent.BulletPart.KillSound:Stop()
							script.Parent.BulletPart.Hit:Stop()
						else
							script.Parent.DAMAGE:FireClient(player,game.Players:GetPlayerFromCharacter(model),100)
							script.Parent.BulletPart.Hit:Play()
							if model.Humanoid.Health ~= 0 then
								script.Parent.BulletPart.KillSound:Play()
								game.ReplicatedStorage.KillGui:FireClient(player, model)
								model.Humanoid.Health -= 100
								player.leaderstats.Kills.Value += 1
								game.ReplicatedStorage.Alive.Value -= 1
							end
							wait(2)
							script.Parent.BulletPart.KillSound:Stop()
							script.Parent.BulletPart.Hit:Stop()
				end

					end

				end
			else
				wait(difference)
				if hp.Parent ~= script.Parent then
				end
			end
		else
			wait(difference)
			if hp.Parent ~= script.Parent then
			end
			end
		end

ok the reason why its not working is because you put the if statement for the left arm / right arm then right after you below checked for the left leg / right leg when you should be doing an else statement for it. I couldn’t figure out the code bc it’s not mine, but I think thats the reason

So something like this

if model or hp.Name == "Left Arm" or hp.Name == "Right Arm" and model:FindFirstChild("Humanoid") then
	script.Parent.DAMAGE:FireClient(player,game.Players:GetPlayerFromCharacter(model),85)
	task.wait(.1)
	script.Parent.BulletPart.Hit:Play()

	if model.Humanoid.Health ~= 0 and model.Humanoid.Health <= 15 then
		script.Parent.BulletPart.KillSound:Play()
		player.leaderstats.Kills.Value += 1
		game.ReplicatedStorage.KillGui:FireClient(player, model)
		game.ReplicatedStorage.Alive.Value -= 1

	elseif model.Humanoid.Health ~= 0 and model.Humanoid.Health >= 15 then
		model.Humanoid.Health -= 85
	end

elseif model or hp.Name == "Left Leg" or hp.Name == "Right Leg" and model:FindFirstChild("Humanoid") then
	script.Parent.DAMAGE:FireClient(player,game.Players:GetPlayerFromCharacter(model),85)
	task.wait(.1)
	script.Parent.BulletPart.Hit:Play()

	if model.Humanoid.Health ~= 0 and model.Humanoid.Health <= 15 then
		script.Parent.BulletPart.KillSound:Play()
		player.leaderstats.Kills.Value += 1
		game.ReplicatedStorage.KillGui:FireClient(player, model)
		game.ReplicatedStorage.Alive.Value -= 1

	elseif model.Humanoid.Health ~= 0 and model.Humanoid.Health >= 15 then
		model.Humanoid.Health -= 85
	end
end
	

Note: Sounds should be played locally as well.

1 Like