Best way to make Melee System?

So right now, I’m working on a game that involves close-range melee weapon combat. I’ve been searching for weeks now, how I could make this system optimized and accurate because right now I am using things like .Touched, but just keep getting stuck so I’m making a post for this.

  • I have heard of RaycastHitbox and I am planning to use this.
  • Everything is handled on the Server, I’ve heard it isn’t ideal to do this but I’m not sure how to handle effects and the hitboxes on the client.
  • The script has been causing some performance issues
Mainscript Code
local SlapHandler = {}

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local BadgeService = game:GetService("BadgeService")
local Debris = game:GetService("Debris")

local Modules = ReplicatedStorage:FindFirstChild("Modules")
local Variants = script.Variants

local PassModule = require(Modules.GamepassOwnershipModule)
local HitEffect = require(Modules.HitEffectModule)
local Manager = require(game:GetService("ServerStorage").DataStorage.ProfileManager)
local XPModule = require(game:GetService("ServerScriptService").Modules.used.RewardXP)
local Configuration = require(script.Configurations)

function sanityCheck(player, enemy)
	if not player or not enemy then return false end
	local plrHum = player.Character and player.Character:FindFirstChild("Humanoid")
	local eHum = enemy:FindFirstChild("Humanoid")

	local plrHRP = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
	local eHRP = enemy:FindFirstChild("HumanoidRootPart")
	if not plrHRP or not eHRP then return false end

	if plrHum and eHum and not enemy:FindFirstChild("Invincibility") and enemy.Name ~= player.Name then
		if (plrHum and eHum).Health > 0 then
			-- passed enemy req, moving to proximity
			if (plrHRP.Position - eHRP.Position).Magnitude <= Configuration.MaxMagnitude then
				return true -- passed check
			end
		end
	end

	return false
end

local function getData(player: Player): table
	local profile = Manager:GetData(player)
	if profile then
		return profile.Data
	end
end

function addWarn(msg : String)
	warn("[SlapHandler]:", msg.. ".")
end

function SlapHandler:setupConnections(Tool : Tool, Config : Configuration)
	local debounce = false
	local player = Players:GetPlayerFromCharacter(Tool.Parent)
	if not player then addWarn("No player from tool parent") return end
	
	local plrChar = player.Character or player.CharacterAdded:Wait(5) or Tool.Parent
	local humanoid = plrChar:FindFirstChild("Humanoid")
	if not plrChar or humanoid then addWarn("Missing character elements") return end
	
	local RagTime = Config:GetAttribute("RagTime")
	local Cooldown = Config:GetAttribute("Cooldown")
	local RagTime = Config:GetAttribute("RagTime")
	local Anim = Config.SlapAnimation
	local SlapSFX = Tool.Glove.SlapSFX
	local AnimTrack

	local function onActivated()
		if debounce then return end
		debounce = true
		
		local humanoid = Tool.Parent:FindFirstChild("Humanoid")
		if not AnimTrack then
			AnimTrack = humanoid:LoadAnimation(Anim)
		end
		AnimTrack:Play()
		task.wait(Cooldown)
		debounce = false
	end

	local function onHitboxTouched(hit)
		if not debounce then return end
		if sanityCheck(player, hit.Parent) then
			SlapSFX:Play()

			local eChar = hit.Parent
			local eHumanoid = eChar:FindFirstChild("Humanoid")
			local plrHRP = plrChar:FindFirstChild("HumanoidRootPart")
			local eRootPart = eChar:FindFirstChild("HumanoidRootPart")
			local pHead = plrChar:FindFirstChild("Head")
			local eHead = eChar:FindFirstChild("Head")
			HitEffect.playHitEffect(player, eChar)

			if eChar:FindFirstChild("HitChar") then
				eChar.HitChar:Destroy()
			end

			local hitCharValue = Instance.new("StringValue", eChar)
			hitCharValue.Name = "HitChar"
			hitCharValue.Value = Tool.Parent.Name
			task.delay(19, function()
				hitCharValue:Destroy()
			end)

			local storeJumpPower = eHumanoid.JumpHeight
			local storeWalkSpeed = eHumanoid.WalkSpeed
			local dmgPercent = Configuration.DamagePercentage

			local ragdollValue = eChar:FindFirstChild("RagEnabled")
			if ragdollValue then
				ragdollValue.Value = true
			end

			eHumanoid.WalkSpeed = 0
			eHumanoid.JumpHeight = 0
			eHumanoid:TakeDamage(math.ceil(Config.Power.Value * dmgPercent))

			local data = getData(player)
			if data then
				local gloveStat = data["Statistics"]["GloveStats"][Tool.Name]
				if gloveStat then
					gloveStat.DamageDealt += math.ceil(Config.Power.Value * dmgPercent)
				end
				data["Statistics"].TotalDamage += math.ceil(Config.Power.Value * dmgPercent)
			end

			local lookVector = plrHRP.CFrame.LookVector
			local upVector = plrHRP.CFrame.UpVector
			local knockbackVelocity = lookVector * Config.Power.Value + upVector * (Config.Power.Value / 2)

			local force = Instance.new("BodyVelocity", eRootPart)
			force.Velocity = knockbackVelocity
			force.MaxForce = Vector3.new(1, 1, 1) * math.huge
			force.P = 1250

			local rotation = Instance.new("BodyAngularVelocity", eRootPart)
			rotation.AngularVelocity = Vector3.new(math.random(), math.random(), math.random()).Unit * math.pi * math.random(2, 5)
			rotation.MaxTorque = Vector3.new(1, 1, 1) * math.huge
			rotation.P = 5000
			
			if Variants[Tool.Name] then
				require(Variants[Tool.Name].Activate(
					{
						player = player,
						enemy = eChar,
						bodyVelocity = force,
					})
				)
			end
			
			task.delay(0.15, function()
				force:Destroy()
				rotation:Destroy()
			end)
			
			task.wait(RagTime)
			if ragdollValue then
				ragdollValue.Value = false
			end

			eHumanoid.WalkSpeed = storeWalkSpeed
			eHumanoid.JumpHeight = storeJumpPower
		end
	end

	Tool.Activated:Connect(onActivated)
	Tool.GloveHitbox.Touched:Connect(onHitboxTouched)
end

return SlapHandler

Handling everything on the server will cause lag as many players will be using the melee system at the same time. The best way to do this is:
Client: On player click, play the animation and then do a hitbox check and if they hit the player, play a sound effect. Ask the server to check if the other player was definitely hit with a remote event.
Server: When the remote event is fired, do sanity checks (Is player close enough to the other person to hit them?) and then apply knockback and damage.

BodyVelocity is deprecated, don’t use it. For knockback, try applyImpulse on the player

what about adding new debounce to function onHitboxTouched so whene player hit two players in same time only one player take damage

Thanks! But I am worried about keeping the client script inside the tools because of bugs, will this be a problem?

It shouldn’t be, to my knowledge I don’t believe any scripts parented to the tool will run until the player equips them.

Do NOT handle visual effects on the server. Instead, just send a remote to all clients to do the effect on their own computer, because excess parts on the server just cause slowness to the server. Remember to ratelimit effects too, because clients can lag too.

All scripts on tools run from the moment they are a descendant of workspace. Though, tool.Equipped fires when the tool’s equipped..

So it looks to me like the script is a server script, one way to do effects is to have a remote event in the tool that fires for all clients. And doing hitboxes on the client and whatnot aren’t necessary, this system works fine aswell. Again, like many things, the lag caused by using the melee system by a lot of players is nearly non existent and u are expected to be doing most things on the server anyway. And I’m not sure why u would restrict the hitbox to hitting only one player, but I’d suggest going with @MdoxxxDSQa 's method. Also there’s nothing wrong with using body velocity, a lot of games still use it but I’d definetly suggest using apply impulse method on parts instead.

This won’t be a problem. tools are mostly designed to use local scripts to provide instantaneous feedback to input, so there won’t be any bugs specifically due to this.