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