Help with optimizing sword damage

Include a standalone, bare-bones rbxl file with only the code you want reviewed.
(can’t include rbxl file as it involves animations I made)

Provide an overview of:
What does the code do and what are you not satisfied with?
I made a sword that is meant to deal damage to designated mobs, but I find it very scuffed and does inconsistent damage (sword sometimes hits twice in one go), and also very reliant on the animations

What potential improvements have you considered?
I tried using a damage box system instead, attaching an invisible box in front of the player to do damage whenever the sword is used; it solves the reliance on animation problem, but I still don’t like the way the damage script has to constantly turn on and off, with lag sometimes causing it to do more damage than intended per hit

How (specifically) do you want to improve the code?
I have no idea (I’m not particularly good at scripting yet, I barely got this code to work)

serverscript parented to the tool object itself, plays animations and activates damage script:

local Debounce = false
local order = 1
local damage = script.Parent["SwordModel"].Blade.damage

script.Parent.Activated:Connect(function()
	
	
	local animation1 = script.Swing1
	local animation2 = script.Swing2
	local animation3 = script.Swing3
	local humanoid = script.Parent.Parent.Humanoid
	local emptyslash = script.Parent["SwordModel"].Blade.Knife_Slash

	if Debounce == false and order == 1 then
		
		Debounce = true
		order = 2
		damage.Enabled = true
	   local animationTrack = humanoid:LoadAnimation(animation1)
		animationTrack:Play()
		emptyslash:Play(1)
		wait(0.2)
		damage.Enabled = false
	   
	   wait(0.1)
		Debounce = false
		
	elseif Debounce == false and order == 2 then
		
		Debounce = true
		order = 3
		damage.Enabled = true
		local animationTrack = humanoid:LoadAnimation(animation2)
		animationTrack:Play()
		emptyslash:Play(1)
		wait(0.2)
		damage.Enabled = false

		wait(0.1)
		Debounce = false
		
	elseif Debounce == false and order == 3 then

		Debounce = true
		order = 1
		damage.Enabled = true
		local animationTrack = humanoid:LoadAnimation(animation3)
		animationTrack:Play()
		emptyslash:Play(1)
		wait(0.2)
		damage.Enabled = false

		wait(0.2)
		Debounce = false
		
		
		
		
	end
		
	
end)

serverscript parented to the sword’s damage-dealing part, checks if hit humanoid contains mob tag:

local Debounce = false
local slice = script.Parent["sword slash"]

function onTouch(part) 
	local humanoid = part.Parent:FindFirstChild("Mob") 
	if (humanoid ~= nil) and Debounce == false then	
		Debounce = true
		humanoid.Parent.Humanoid.Health = humanoid.Parent.Humanoid.Health - 2
		slice:Play()
		wait(0.2)
		Debounce = false
	end 
end

script.Parent.Touched:connect(onTouch)
1 Like
  1. You may use CollectionService tags, they serve same purpose. (and doesn’t use Instances)
  2. Are you sure you’re adding ‘Mob’ tag to the character?
  1. How do I use Collectionservice in this?
  2. It’s a StringValue in the mob, the damage script checks for it to determine whether it can be damaged

What I’m looking for is a consistent way to ensure that every mob hit by the sword during its attack animation to be dealt precisely one hit (toggling the damage script with a debounce doesnt do that very well)

I thought you tagged using value objects, sorry.
What if you disconnect the OnTouch event on hit, and disable/enable the script from the server?

Thanks, that works well
robloxapp-20240301-2015097.wmv (1.6 MB)

But now I still have another issue of the sword being unable to deal damage to multiple entities at once, while at the same time ensuring each entity only gets hit once

1 Like