Hello, everyone! I recently made a damage script for a weapon, and it seems to only work for a single target. Is there any way to make the weapon deal damage to every single target touched?
Example Video: Imgur: The magic of the Internet
LocalScript (inside the tool):
-- WEAPON COMBAT SCRIPT
-- BY Nochit(qMrSpooky)
-- Category: 2Hands; Hammer
--/ VARIABLES --
local player = game.Players.LocalPlayer
local Tool = script.Parent
local db = false
local canDmg = false
local blade = Tool:WaitForChild("Damage")
local DamageEvent = game:GetService("ReplicatedStorage"):WaitForChild("DamageEvent")
repeat wait(1) until player.Character
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
-- VARIBALES /--
--/ SETTINGS --
local dmgvalue = x -- Damage the weapon deals to Humanoids.
local delayTime = x -- Time the weapon waits before being able to attack again.
local shakeTime = x -- Time the screen shakes when weapon hits.
-- SETTINGS /--
--/ CONFIG --
local function shakeCamera(length)
(camera shake code)
end
local function onTouch(partOther)
local humanOther = partOther.Parent:FindFirstChild("Humanoid")
if not humanOther then return end
if humanOther.Parent == Tool then return end
DamageEvent:FireServer(humanOther, dmgvalue)
end
local function Hurt()
if canDmg == true then
blade.Touched:Connect(function(hit)
if (not canDmg) then return end
canDmg = false
onTouch(hit)
end)
end
end
-- CONFIG /--
--/ ANIMATIONS --
(animation category, not needed here)
-- ANIMATIONS /--
--/ WEAPON CODE --
Tool.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
if db == false then
db = true
canDmg = false
animtrack2:Play()
animtrack2.Stopped:wait()
animtrack:Play()
wait(0.7)
canDmg = true
Hurt()
shakeCamera(shakeTime) -- Optional. Only for "Heavy" Weapons
animtrack.Stopped:wait()
wait(delayTime)
db = false
end
end)
end)
Tool.Unequipped:Connect(function()
animtrack:Stop()
animtrack2:Stop()
end)
-- WEAPON CODE /--
The way I tend to handle this is not to use touched at all. Instead, I use maths to calculate all the distance to all players from the centre point where you want the damage, and then only damage those that are within the range you specify. While this sounds complicated, it’s just a case of doing a quick players:getchildren() for loop and checking the magnitude of the vector between the centre point and their humanoidrootpart.
To attack multiple enemies, create a table of what you can’t damage. Like so:
local Damaged = {}
local function Hurt()
if canDmg == true then
blade.Touched:Connect(function(hit)
if Damaged[hit.Parent.Name] or not canDmg then return end
Damaged[hit.Parent.Name] = true
onTouch(hit)
end)
--then you need to wait some time to make it not damageable.
task.wait(.25)
canDmg = false
Damaged = {}
end
end
how would I go about doing that?
EDIT: Like this?
Server Script in ServerScriptService:
--/ SETTINGS --
local dmgvalue = 20 -- Damage the weapon deals to Humanoids.
local delayTime = 5 -- Time the weapon waits before being able to attack again.
local shakeTime = 0.6 -- Time the screen shakes when weapon hits.
-- SETTINGS /--
localScript in tool:
local SScript = game.ServerScriptService.Damage --(the server script)
local function onTouch(partOther)
local humanOther = partOther.Parent:FindFirstChild("Humanoid")
if not humanOther then return end
if humanOther.Parent == Tool then return end
DamageEvent:FireServer(humanOther, SScript.dmgvalue)
end
local function Hurt()
if canDmg == true then
blade.Touched:Connect(function(hit)
if Damaged[hit.Parent.Name] or not canDmg then return end
Damaged[hit.Parent.Name] = true
onTouch(hit)
end)
task.wait(.25)
canDmg = false
Damaged = {}
end
end
Doesn’t even seem to register my clicks anymore. is there anything I should write in the Damaged table?
You can use Region3s to create a hitbox around your tool’s hitpart and then do a loop that runs for the time of the animation that flags and damages parts that have been hit (while including a cache/storage system to prevent accidental 1shotting)
I saw something about Region3, but I do not know how to make it, I’m not a very experienced scripter and I can’t wrap my head around region3 since I didn’t understand the code