Gun doesnt deal damage to shield

game.ReplicatedStorage.LaserFireEvent.OnServerEvent:Connect(function(player, mousePos)
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances= {player.Character}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	
	local result = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position)*300, Params)
	
	if result then
		local hitPart = result.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")
		
		if model then
			if model:FindFirstChild("Humanoid") then
				local tool = model:FindFirstChildOfClass('Tool')
				if not tool or tool.Name ~= 'Shield' then
					model.Humanoid.Health -= 30 or tool:FindFirstChild("Humanoid").Health -= 30
				end
			end
		end
	end
end)

As title says, I want gun to deal damage to shield, but it doesnt work (it deals damage to player). What wrong did I do?

1 Like

It looks like you are trying to damage both the player and their shield, but currently the code is only checking if the hit part’s ancestor model has a Humanoid and a Shield tool. If the tool is not present or its name is not “Shield”, the code damages the player’s health.

To make the gun deal damage to the shield instead of the player, you could change the code to check if the hit part is a Shield instead of checking if its ancestor model has a Shield tool.

2 Likes

I want it, so if hit parent is shield it deals damage to shield and to player if hit parent is player

Seeing the sheild in the explorer would be giving a lot of hints about what’s the problem could we see it?

1 Like

I just left home so I cant give you SS, but it looks like:

Shield (tool):
Humanoid
Script
Handle

im very convinced that the problem lies on the line ‘if not tool or’ im assuming thats being triggered, also where it changes the shield’s health needs to be in a seperated elseif i believe

1 Like

Try changing this to:

if not tool then
    model.Humanoid.Health -= 30
    return
end

if tool.Name ~= "Shield" then
    model.Humanoid.Health -= 30
    return
end

tool:FindFirstChild("Humanoid").Health -= 30

If there are any typos then my bad, I’m on mobile

And expect hours late response, I’m asleep

1 Like

not working

dsddsfdfdsfsdfdfsfdf

I still need help. It is pretty urgent for me right now.

you are only saying wjat needs to happen if the player is not holding a shield.

make an else statement and write some code in there to damage the shield

if not tool or tool.Name ~= “Shield” then
— write player damage code
else
— write shield damage code
end

1 Like

Are there any errors or just the same result?

You should say what happened next or else I won’t be able to help

1 Like

What should I say what happened next? Nothing happened, player takes damage, but shield dont

Neither happening here too

ddsf

Try this:

game.ReplicatedStorage.LaserFireEvent.OnServerEvent:Connect(function(player, mousePos)
local Params = RaycastParams.new()
Params.FilterDescendantsInstances= {player.Character}
Params.FilterType = Enum.RaycastFilterType.Blacklist

local result = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position)*300, Params)

if result then
    local hitPart = result.Instance
    local model = hitPart:FindFirstAncestorOfClass("Model")
    
    if model then
        if model:FindFirstChild("Shield") then
            local shield = model.Shield
            shield.Health -= 30
        elseif model:FindFirstChild("Humanoid") then
            model.Humanoid.Health -= 30
        end
    end
end
end)
1 Like

Maybe try checking the explorer while you’re testing (giving a screenshot of that would be good)

1 Like

What do you mean by that?

Dhdhhdhh

I also tried this, it isnt working as it should

Nevermind, It worked. I dunno how it wasnt working and now it does

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.