I am using the RaycastHitbox module for my melee system, and I’m doing the raycasting on the client and doing damage on the server, but how can I add a server-sided check to detect to make sure they are not attacking too fast without making the melee only hit one target per swing?
Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RaycastHitbox = require(ReplicatedStorage.Modules.RaycastHitboxV4)
local localPlayer = Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local container = script.Parent
local tool = container.Parent
local toolRemotes = tool.Remotes
local Configuration = require(tool.Configuration)
local hitRemote = toolRemotes:WaitForChild("Hit")
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character, tool}
raycastParams.CollisionGroup = "IgnorePlayers"
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local raycastHitbox = RaycastHitbox.new(tool.Handle)
raycastHitbox.RaycastParams = raycastParams
raycastHitbox.Visualizer = true
local function onActivated()
raycastHitbox:HitStart(Configuration.ATTACK_DURATION)
task.delay(Configuration.ATTACK_DURATION, function()
raycastHitbox:HitStop()
end)
end
local function onHit(hit: Instance, humanoid: Humanoid)
hitRemote:FireServer(hit, humanoid)
end
tool.Activated:Connect(onActivated)
raycastHitbox.OnHit:Connect(onHit)
Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local validateHit = require(ReplicatedStorage.Modules.Utility.validateHit)
local container = script.Parent
local tool = container.Parent
local Configuration = require(tool.Configuration)
local lastHit = os.clock()
local function onHitEvent(playerThatFired: Player, hit: Instance, humanoid: Humanoid)
if not validateHit(tool, playerThatFired, hit, humanoid) then
return
end
if math.abs((os.clock() - lastHit)) < Configuration.ATTACK_DURATION then
warn("Min")
return
end
humanoid:TakeDamage(Configuration.DAMAGE)
lastHit = os.clock()
end
tool.Remotes.Hit.OnServerEvent:Connect(onHitEvent)