Advertisement
Guest User

SwordScript

a guest
May 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.17 KB | None | 0 0
  1. -- Variables for the sword and booleans
  2. local sword = script.Parent
  3. local canAttack = true
  4. local canDamage = false
  5.  
  6. -- Function that runs when any part touches the blade
  7. function onTouch(part)
  8.    
  9.     local character = part.Parent
  10.     local player =  game.Players:GetPlayerFromCharacter(character)
  11.    
  12.     -- Check if player exists and can damage is true, then deal the damage
  13.     if player then
  14.         if canDamage == true then
  15.             character.Humanoid:TakeDamage(20)
  16.         end
  17.        
  18.     end
  19.    
  20. end
  21. sword.Blade.Touched:Connect(onTouch)
  22.  
  23. -- Attack function that swings sword and activates damage
  24. function Attack()
  25.    
  26.     -- Sword swing animation that is located in the player that holds the sword
  27.     local attackAnim = sword.Parent.Humanoid:LoadAnimation(script.SwordSwing)
  28.    
  29.     -- If statement that attacks only when canAttack is true
  30.     if canAttack == true then
  31.         -- Play attack animation and set canAttack to false
  32.         attackAnim:Play()
  33.         canAttack = false
  34.        
  35.         -- Sword damageable for 0.5s
  36.         canDamage = true
  37.         wait(0.5)
  38.        
  39.         -- Deactivate sword damage for 0.25s before being able to do damage again
  40.         canDamage = false
  41.         wait(0.25)
  42.         canAttack = true
  43.  
  44.     end
  45. end
  46. sword.Activated:Connect(Attack)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement