Touched event sometime not registering, when used on ServerSide

As you can see that the first few hit does registered, and the last doesnt, I need help, here is the code block

local plrsHit = {}
local CanDamage = false

local debounce = false

local function slash()
	if debounce then return end
	debounce = true
	plrsHit = {}
	windupAnim:Play()
	windupAnim.Stopped:Wait()
	CanDamage = true
	slashAnim:Play()
	slashAnim.Stopped:Wait()
	CanDamage = false
	wait(2)
	debounce = false
end

script.Parent.Blade.Touched:Connect(function(touch)
	if not CanDamage then return end
	if game.Players:GetPlayerFromCharacter(touch.Parent) and not plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] then
		plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = true
		touch.Parent.Humanoid:TakeDamage(20)
	end
end)
1 Like

The .Touched event, unfortunately, can be quite inconsistent sometimes. I would recommend you use a Ray to detect when the opposing player is directly in front of character.

You can implement Raycasting, which will allow you to find the character in the swords path.

Here is a basic example of what I mean:


local function slash()
	if debounce then return end
	debounce = true
	plrsHit = {}
	windupAnim:Play()
	windupAnim.Stopped:Wait()
	CanDamage = true
	slashAnim:Play()
   
    NewRay = Ray.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.CFrame.LookVector * ShootOutLength) --creates a new ray that extends out forward ShootOutLength studs from the HumanoidRootPart of the character
    touch = workspace:FindPartOnRayWithIgnoreList( 
NewRay,{character}) --get the part touching ray, with ignoring the character and it's defendants
  
    if game.Players:GetPlayerFromCharacter(touch.Parent) and not plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] then
		plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = true
		touch.Parent.Humanoid:TakeDamage(20)
	end
	slashAnim.Stopped:Wait()
	CanDamage = false
	wait(2)
	debounce = false
end
Resources:

Raycasting | Documentation - Roblox Creator Hub

WorldRoot | Documentation - Roblox Creator Hub

WorldRoot | Documentation - Roblox Creator Hub

2 Likes

I actually did this, I used @TeamSwordphin module Raycast Hitbox 4.01: For all your melee needs!

That module is a very good option if you don’t want to learn how to do it yourself. It is very consistent, and works perfect in most cases.

1 Like