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!