Roblox Gun Damage Help

for some reason on this gun system i made if the player clicks with the gun on an accessory and not the player itself the player cant take damage…

Heres the server damage script:

ShootRem.OnServerEvent:Connect(function(player, target)
	target.Humanoid:TakeDamage(dmg)
end)

Local:

		if target.Parent:FindFirstChild("Humanoid") then
			script.Parent.Shoot:FireServer(target.Parent)
		end

You shouldn’t do hit detection on the client btw, but anyways use FindFirstAncestorWhichIsA() then find the humanoid.

This is because accessories are under the hat so it’s not a child of the character.

what do i use then? and where do i use FindFirstAncestorWhichIsA()

What if your target is not a humanoid, will it still shoot? Also script.parent.shoot:Fireserver()…where is this remote event?

yeah it does shoot, dont worry

just need help with the accessory thing

		local target = mouse.Target
		if target.Parent:FindFirstAncestorWhichIsA("Humanoid") then
			script.Parent.Shoot:FireServer(target.Parent)
		end

this does nothing

Hello.

The most efficient way you can use is “Raycasting”.

Here is an example snippet of raycasting including your code:

Local Script:

local tool = script.Parent -- Change this to your tool location.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

tool.Activated:Connect(function()
    script.Parent.Shoot:FireServer(mouse.Hit.Position)
end)

Server Script:

script.Parent.Shoot.OnServerEvent:Connect(function(player, mousePos)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {player.Character}

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

if raycast.Instance and raycast.Instance.Parent then
    if raycast.Instance.Parent:FindFirstChildOfClass("Humanoid") then
         raycast.Instance.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(10) -- Write your desired damage here.
    end
end

end)

If this ever works for you, you’re welcome, and make sure to mark it as the solution! :slight_smile:

1 Like

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