How can i improve my slashing sword script?

so how could i improve this script by making it smoother or smaller?

local Tool = script.Parent
local player = Tool.Parent.Parent
local humanoid = player.Character.Humanoid
local swings = script:GetChildren()
local Enable = true
local CanDamage = false

Tool.Activated:Connect(function()
if Enable then
Enable = false
local Swing = swings[math.random(1,#swings)]
humanoid:LoadAnimation(Swing):Play()
CanDamage = true
wait(0.7)
Enable = true
CanDamage = false
end
end)

Tool.HitBox.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild(“Humanoid”)
if hum and hum ~= humanoid then
if CanDamage then
hum:TakeDamage(25)
CanDamage = false
end
end
end)

thx if you could help :slight_smile:

Hi,

I’m guessing that “HitBox” is a part within “Tool” and that you only want your sword to be hitting one enemy per swing? If so, it’s looking good, I don’t see too many ways you can shorten the script, and I don’t see why you need to. Have you got any problems with it? Is it not functioning properly in game?

I suppose there are a few one-time variables that you could get rid of, since you’re only defining them to be used one time. Here is my take on your script:

local Tool = script.Parent
local humanoid = Tool.Parent.Parent.Character:WaitForChild("Humanoid")
local Enable, CanDamage = true, false

Tool.Activated:Connect(function()
	if Enable then
		Enable = false
		humanoid:LoadAnimation(script:GetChildren()[math.random(1,#script:GetChildren())]):Play()
		CanDamage = true
		wait(0.7)
		CanDamage, Enable = false, true
	end
end)

Tool.HitBox.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild(“Humanoid”)
	if hum and hum ~= humanoid then
		if CanDamage then
			hum:TakeDamage(25)
			CanDamage = false
		end
	end
end)
2 Likes

Thanks but for more and better practice for expirence so i can get better and maybe understand it im fairly new to scripting tho so it could help out thanks.

1 Like