I’ve recently started working with the new RayCast API since the latter is now deprecated. However, I’ve ran into notable issues with my Swords / Melee weapons - Using the first API method, the damage is seamless and no issues. However, after upgrading to the newer API, it seems not to work (unless the Player themselves are making physical contact with the NPC.)
Below is a brief video, Green indicates old API, whereas Red is new API.
I have also provided the coding for both attack functions below. [Old & New APIs]
Old API
Run = game:GetService("RunService").Heartbeat:Connect(function()
if Slash1.IsPlaying then
local Origin = Tool["HitBox"]["AT0"].WorldPosition
local Direction1 = Tool["HitBox"]["AT1"].WorldPosition
local Direction2 = (Origin - Direction1).Unit * 2
local R = Ray.new(Origin, Direction2 * ((Origin - Direction1).Magnitude))
local PartFound, Pos = workspace:FindPartOnRay(R, Player.Character)
if PartFound then
if PartFound.Parent:FindFirstChild("Humanoid") ~= nil then
Victim = PartFound.Parent["Humanoid"]
elseif PartFound.Parent.Parent:FindFirstChild("Humanoid") ~= nil then
Victim = PartFound.Parent.Parent["Humanoid"]
end
if Victim then
Run:Disconnect()
Victim:TakeDamage(math.random(5, 20))
Victim = nil
end
end
end
end)
New API
Run = game:GetService("RunService").Heartbeat:Connect(function()
if Slash1.IsPlaying then
local Origin = Tool["HitBox"]["AT0"].WorldPosition
local Direction1 = Tool["HitBox"]["AT1"].WorldPosition
local Direction2 = (Origin - Direction1).Unit * 2
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(Origin, Direction2, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
if hitPart.Parent:FindFirstChild("Humanoid") ~= nil then
Victim = hitPart.Parent["Humanoid"]
elseif hitPart.Parent.Parent:FindFirstChild("Humanoid") ~= nil then
Victim = hitPart.Parent.Parent["Humanoid"]
end
if Victim then
Run:Disconnect()
Victim:TakeDamage(math.random(5, 20))
Victim = nil
end
end
end
end)