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)