So I am trying to make a sword system and I can’t quite figure out how to add Blocking and Parrying. Here is my code:
Sword Script:
r = game:service("RunService")
local damage = 0
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 -- happens when bullet hits sword
local humanoid = hit.Parent:findFirstChild("Humanoid")
local vCharacter = Tool.Parent
local vPlayer = game.Players:playerFromCharacter(vCharacter)
local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
if humanoid~=nil and humanoid ~= hum and hum ~= nil then
-- final check, make sure sword is in-hand
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)
else
enabledToDamage = true
end
else
enabledToDamage = true
end
else
enabledToDamage = true
end
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
force = Instance.new("BodyVelocity")
force.velocity = Vector3.new(0,10,0) --Tool.Parent.Torso.CFrame.lookVector * 80
force.Parent = Tool.Parent.Torso
wait(.5)
force.Parent = nil
wait(.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.Humanoid
if humanoid == nil then
print("Humanoid not found")
return
end
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)
connection = sword.Touched:connect(blow)
Animation Script:
-- 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)
The game is R6. If anyone can add anything to my scripts it would be a huge help. Thank you!