Something wrong with my gun script

I made a gun script that deals damage to player’s Humanoid
but the problem is that I want it to deal double the damage if the Raycast hits the player’s head just like a headshot
Here is my script

local DamagePlayer = game.ReplicatedStorage.DamagePlayer
local range = 300
DamagePlayer.OnServerEvent:Connect(function(player, MousePosition, Origin)
	script.Parent.GunShot:Play()
	local direction = (MousePosition - Origin).Unit * range
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {player.Character}
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	local results = workspace:Raycast(Origin, direction)
	if results then
		local Character = results.Instance.Parent
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid:TakeDamage(20)
		elseif Character:FindFirstChild("Head") then
			Humanoid:TakeDamage(30)
		end
	end
end)

I don’t want to change anything in my script I just want a solution to my Problem
What should i do to fix this?
Is there a specific function for this?

You need to check if results.Instance is hitting ‘head’ or not by doing this:

if results.Instance == "Head" then
   Humanoid:TakeDamage(30)
end

That did not work it still deals 20.

ok, i wasn’t on studio.

if results.Instance.Name == "Head" then
   Humanoid:TakeDamage(30)
end

i forgot to check the name…

That also did not work what else?

Try this

if results then
	local Character = results.Instance.Parent
	local Humanoid = Character:FindFirstChild("Humanoid")
	if Humanoid then
		if results.Instance.Name == "Head" then
			Humanoid:TakeDamage(30)
		else
			Humanoid:TakeDamage(20)
		end
	end
end

You need to change the if statement around because the if Humanoid then statement will always be done if you attack the character

1 Like

That worked thank you very much.

1 Like