As you can see I am trying to hit the npc but the ray is going in a different direction
My script
local function performRaycast(character, range)
local startPosition = character.PrimaryPart.Position
local endPosition = startPosition + character.PrimaryPart.CFrame.LookVector * range
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {character}
local result = workspace:Raycast(startPosition, endPosition, raycastParams)
if result then
local distance = (startPosition - result.Position).Magnitude
local p = Instance.new("Part")
p.Anchored = true
p.CanCollide = false
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.lookAt(startPosition, result.Position)*CFrame.new(0, 0, -distance/2)
p.Parent = workspace
end
return result
end
local function applyDamageToNPC(hitCharacter, damage)
local humanoid = hitCharacter:FindFirstChild("Humanoid")
if humanoid and not hitCharacter:FindFirstChild("Player") then
print("Applying damage to NPC")
humanoid:TakeDamage(damage)
return true
else
print("Humanoid not found or hitCharacter is a player")
end
return false
end
local function playHitReaction(hitCharacter, hitReactionAnimationId)
local humanoid = hitCharacter:FindFirstChild("Humanoid")
if not hitCharacter or not humanoid then return end
local animation = Instance.new("Animation", hitCharacter)
animation.AnimationId = "rbxassetid://" .. hitReactionAnimationId
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack.Priority = Enum.AnimationPriority.Action
animationTrack:Play()
end
LuffiModule.Punch = function(player, punchIndex, punchRange, punchDamage)
local punchAnimationId = LuffiModule.PunchAnimations[punchIndex]
playPunchAnimation(player, punchAnimationId)
wait(0.1) -- You can adjust this delay for when the raycast should be performed during the animation
local character = player.Character
local result = performRaycast(character, punchRange)
if result and result.Instance.Parent:IsA("Model") then
local hitCharacter = result.Instance.Parent
print("Raycast hit:", hitCharacter.Name)
local npcHit = applyDamageToNPC(hitCharacter, punchDamage)
if npcHit then
print("Damage applied to:", hitCharacter.Name)
local hitReactionAnimationId = LuffiModule.HitReactionAnimations[punchIndex]
playHitReaction(hitCharacter, hitReactionAnimationId)
end
else
print("Raycast did not hit a model")
end
end