I have a custom sword and I wanted to add a block but I can’t find a way to make it without breaking my entire sword. If you know how to add this please help me. Here are the scripts:
SwordScript:
r = game:GetService("RunService")
local damage = 0
local slash_damage = 10 -- Define your damage values
local lunge_damage = 20 -- Define your damage values
sword = script.Parent.Handle
Tool = script.Parent
local damages, values, sounds = {10, 11, 12}, {Tool.PlaySlash, Tool.PlayThrust, Tool.PlayOverhead}, {Tool.Handle.SlashSound, Tool.Handle.OverheadSound, Tool.Handle.LungeSound}
local enabledToDamage = true
function blow(hit)
if enabledToDamage == false then return end
enabledToDamage = false
if (hit.Parent == nil) then enabledToDamage = true return end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local vCharacter = Tool.Parent
local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)
local hum = vCharacter:FindFirstChild("Humanoid")
if humanoid ~= nil and humanoid ~= hum and hum ~= nil then
local right_arm = vCharacter:FindFirstChild("Right Arm")
if (right_arm ~= nil) then
local joint = right_arm:FindFirstChild("RightGrip")
if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
tagHumanoid(humanoid, vPlayer)
humanoid:TakeDamage(damage)
wait(1)
untagHumanoid(humanoid)
end
end
end
enabledToDamage = true
end
function tagHumanoid(humanoid, player)
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = player
creator_tag.Name = "creator"
creator_tag.Parent = humanoid
end
function untagHumanoid(humanoid)
if humanoid ~= nil then
local tag = humanoid:FindFirstChild("creator")
if tag ~= nil then
tag.Parent = nil
end
end
end
function attack()
damage = slash_damage
script.Parent.Handle.SlashSound:Play()
script.Parent.PlaySlash.Value = not script.Parent.PlaySlash.Value
end
function lunge()
damage = lunge_damage
script.Parent.Handle.LungeSound:Play()
script.Parent.PlayOverhead.Value = not script.Parent.PlayOverhead.Value
local force = Instance.new("BodyVelocity")
force.Velocity = Vector3.new(0, 10, 0)
force.Parent = Tool.Parent:FindFirstChild("Torso")
wait(0.5)
force.Parent = nil
wait(0.5)
damage = slash_damage
end
Tool.Enabled = true
local last_attack = 0
local status = 0
function onActivated()
if not Tool.Enabled then
return
end
Tool.Enabled = false
local character = Tool.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid == nil then
print("Humanoid not found")
Tool.Enabled = true
return
end
local t = r.Stepped:Wait()
if (t - last_attack < 1.5) then
if status == 3 then
status = 0
damage = 0
else
status = status + 1
values[status].Value = not values[status].Value
damage = damages[status]
sounds[status]:Play()
enabledToDamage = true
wait(0.5)
enabledToDamage = false
end
else
status = 0
damage = 0
end
last_attack = t
Tool.Enabled = true
end
function onEquipped()
wait(1/3)
Tool.Handle.UnsheathSound:Play()
end
Tool.Equipped:Connect(onEquipped)
script.Parent.Activated:Connect(onActivated)
local connection = sword.Touched:Connect(blow)
SwordAnimation:
-- Waits for the child of the specified parent
local function WaitForChild(parent, childName)
while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
return parent[childName]
end
local Tool = script.Parent
local sword = Tool:WaitForChild("Handle")
local trail = sword:WaitForChild("Trail")
local HitSounds = "rbxassetid://566593606"
trail.Enabled = false
local Animations = {}
local MyHumanoid
local MyCharacter
local function PlayAnimation(animationName)
if Animations[animationName] then
Animations[animationName]:Play()
end
end
local function StopAnimation(animationName)
if Animations[animationName] then
Animations[animationName]:Stop()
end
end
function OnEquipped(mouse)
MyCharacter = Tool.Parent
MyHumanoid = WaitForChild(MyCharacter, 'Humanoid')
if MyHumanoid then
Animations['EquipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'EquipAnim5'))
Animations['IdleAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'IdleAnim3'))
Animations['OverheadAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'OverheadAnim2'))
Animations['SlashAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'SlashAnim2'))
Animations['ThrustAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'ThrustAnim2'))
Animations['UnequipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'UnequipAnim2'))
end
PlayAnimation('EquipAnim')
PlayAnimation('IdleAnim')
end
function OnUnequipped()
for animName, _ in pairs(Animations) do
StopAnimation(animName)
end
end
Tool.Equipped:connect(OnEquipped)
Tool.Unequipped:connect(OnUnequipped)
WaitForChild(Tool, 'PlaySlash').Changed:connect(
function (value)
--if value then
trail.Enabled = true
PlayAnimation('SlashAnim')
wait(.5)
trail.Enabled = false
--else
-- StopAnimation('SlashAnim')
--end
end)
WaitForChild(Tool, 'PlayThrust').Changed:connect(
function (value)
--if value then
trail.Enabled = true
PlayAnimation('ThrustAnim')
wait(.5)
trail.Enabled = false
--else
-- StopAnimation('ThrustAnim')
--end
end)
WaitForChild(Tool, 'PlayOverhead').Changed:connect(
function (value)
--if value then
trail.Enabled = true
PlayAnimation('OverheadAnim')
wait(.5)
trail.Enabled = false
--else
-- StopAnimation('OverheadAnim')
--end
end)
Thanks!