How to prevent player damage on gun shoot, if he has a shield

Well I want to make a shield for my game to protect players from getting hit.

The issue is, that the raycast ignores the shield and still damages the player.
I tried to check for playing having the shield item equipped, but it wouldn’t work because it would make the player fully bullet proof.

I have a footage of a test with my friend, it shows the problem.


I used alvinblox’s tutorial to make the gun, half of the code is made by myself.

local reloading = false
script.Parent.Fire.OnServerEvent:Connect(function(player,Pos)
	if reloading == false and script.Parent.Configuration.Bullets.Value >= 0 then
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {player.Character}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		script.Parent.Handle.Fired:Play()
		local RayCastResult = workspace:Raycast(script.Parent.Handle.Position,(Pos - script.Parent.Handle.Position)*300,raycastParams)
		if RayCastResult then
			local hit = RayCastResult.Instance
			local Hole = game.ReplicatedStorage.Hole:Clone()
			Hole.Parent = hit.Parent
			Hole.Position = Pos
			Hole.Script.Disabled = false
			local model = hit:FindFirstAncestorOfClass("Model")
			if model then
				if model:FindFirstChild("Humanoid") then
					model.Humanoid.Health -= 30
				end
			end
		end
		script.Parent.Configuration.Bullets.Value -= 1 --not important too
	else
		reload()
	end
end)

function reload()
    --cut this off because not important
end

script.Parent.Reload.OnServerEvent:Connect(reload)

Could anybody help please?

2 Likes

Could you add a print statement to see what the Raycast’s Result actually hit?

1 Like

Check if the hit part of the raycast is the shield, and if it then don’t damage the player

To do this, look at this portion of your code:

local model = hit:FindFirstAncestorOfClass("Model")
if model then
	if model:FindFirstChild("Humanoid") then
		model.Humanoid.Health -= 30
	end
end

Here it is searching all of the hit’s part’s parents for a humanoid. In this case, the riot shield is a tool in which it’s parent is the character which has a humanoid in it. So it does not even take into account the existence of the shield.
The solution would be to add an if statement to check if the hit object is a part of the shield.

local model = hit:FindFirstAncestorOfClass("Model")
if model then
	if model:FindFirstChild("Humanoid") then
		local tool = model:FindFirstChildOfClass('Tool')
		if not tool or tool.Name ~= 'NameOfRiotShieldTool' then --replace with the name of the riot shield tool
			model.Humanoid.Health -= 30
		end
	end
end

This code basically checks the model that has been found (the target player’s character) if they have a tool, since players can only equip one tool at a time. If there isn’t a tool, then go ahead. If there is a tool however, check if the name is not equal to the riot shield name. If it is, then the script will end and will not damage the humanoid. Very simple.

6 Likes

So I have to copy paste this to all my weapons? I mean isn’t there any thing else that will work for everything instead of bunch of copy and paste? like is there an event that fires before player takes damage? or maybe can I increase players health when he is holding shield and decrease it again?