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?
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.
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
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
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)