Advertisement
Guest User

Knife Code that needs fixing (Around 300 Lines Long)

a guest
Apr 5th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.75 KB | None | 0 0
  1. local function WaitForChild(parent, childName)
  2.     while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
  3.     return parent[childName]
  4. end
  5.  
  6.  
  7. local SLASH_DAMAGE = 10
  8. local DOWNSTAB_DAMAGE = 15
  9. local THROWING_DAMAGE = 10
  10. local HOLD_TO_THROW_TIME = .3
  11.  
  12.  
  13. local Damage = 15
  14.  
  15. local MyHumanoid = nil
  16. local MyTorso = nil
  17. local MyCharacter = nil
  18. local MyPlayer = nil
  19.  
  20. local Tool = script.Parent
  21. local Handle = WaitForChild(Tool, 'Handle')
  22.  
  23. local BlowConnection
  24. local Button1DownConnection
  25. local Button1UpConnection
  26.  
  27. local PlayStabPunch
  28. local PlayDownStab
  29. local PlayThrow
  30. local PlayThrowCharge
  31.  
  32. local IconUrl = Tool.TextureId  -- URL to the weapon knife icon asset
  33.  
  34. local DebrisService = game:GetService('Debris')
  35. local PlayersService = game:GetService('Players')
  36.  
  37. local SlashSound
  38.  
  39. local HitPlayers = {}
  40.  
  41. local LeftButtonDownTime = nil
  42.  
  43. local Attacking = false
  44.  
  45. function Blow(hit)
  46.     if Attacking then
  47.         BlowDamage(hit, Damage)
  48.     end
  49. end
  50.  
  51. function BlowDamage(hit, damage)
  52.     local humanoid = hit.Parent:FindFirstChild('Humanoid')
  53.     local player = PlayersService:GetPlayerFromCharacter(hit.Parent)
  54.     if humanoid == nil then
  55.         Handle.WallHit:Play()
  56.         wait(0.1)
  57.         a = game.Workspace:FindFirstChild("Handle")
  58.         if a == nil then
  59.         else
  60.             a:Destroy()
  61.         end
  62.     else
  63.         if humanoid.Health == 0 then
  64.             game.Players.LocalPlayer.leaderstats.Kills.Value = game.Players.LocalPlayer.leaderstats.Kills.Value + 1
  65.             game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value + 15
  66.             Handle.DeadPlayer:Play()
  67.             Handle.DeadSound:Play()
  68.         else
  69.             humanoid.Health = humanoid.Health - THROWING_DAMAGE
  70.             if humanoid.Health <= 0 then
  71.                 game.Players.LocalPlayer.leaderstats.Kills.Value = game.Players.LocalPlayer.leaderstats.Kills.Value + 1
  72.                 game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value + 15
  73.                 Handle.DeadPlayer:Play()
  74.                 Handle.DeadSound:Play()
  75.             end
  76.         end
  77.         wait(0.1)
  78.         if a == nil then
  79.         else
  80.             a:Destroy()
  81.         end
  82.     end
  83.     if humanoid ~= nil and MyHumanoid ~= nil and humanoid ~= MyHumanoid then
  84.         if not MyPlayer.Neutral then       
  85.             -- Ignore teammates hit
  86.             if player and player ~= MyPlayer then
  87.                 return
  88.             end
  89.         end
  90.         -- final check, make sure weapon is in-hand
  91.         local rightArm = MyCharacter:FindFirstChild('Right Arm')
  92.         if (rightArm ~= nil) then
  93.             -- Check if the weld exists between the hand and the weapon
  94.             local joint = rightArm:FindFirstChild('RightGrip')
  95.             if (joint ~= nil and (joint.Part0 == Handle or joint.Part1 == Handle)) then
  96.                 -- Make sure you only hit them once per swing
  97.                 if player and not HitPlayers[player] then
  98.                     TagHumanoid(humanoid, MyPlayer)
  99.                     print("Sending " .. damage)
  100.                     humanoid:TakeDamage(damage)
  101.                     Handle.Splat.Volume = 1
  102.                     Handle.Splat:Play()
  103.                     HitPlayers[player] = true
  104.                 end
  105.             end
  106.         end
  107.     end
  108. end
  109. function TagHumanoid(humanoid, player)
  110.     -- Add more tags here to customize what tags are available.
  111.     while humanoid:FindFirstChild('creator') do
  112.         humanoid:FindFirstChild('creator'):Destroy()
  113.     end
  114.  
  115.     local creatorTag = Instance.new('ObjectValue')
  116.     creatorTag.Value = player
  117.     creatorTag.Name = 'creator'
  118.     creatorTag.Parent = humanoid
  119.     DebrisService:AddItem(creatorTag, 1.5)
  120.  
  121.     local weaponIconTag = Instance.new('StringValue')
  122.     weaponIconTag.Value = IconUrl
  123.     weaponIconTag.Name = 'icon'
  124.     weaponIconTag.Parent = creatorTag
  125.     DebrisService:AddItem(weaponIconTag, 1.5)
  126. end
  127.  
  128. function HardAttack()
  129.     Handle.Attack:Play()
  130.     if PlayStabPunch then
  131.         PlayStabPunch.Value = true
  132.         wait(1.0)
  133.         PlayStabPunch.Value = false
  134.     end
  135. end
  136.  
  137. function NormalAttack()
  138.     Damage = DOWNSTAB_DAMAGE
  139.     KnifeDown()
  140.     Handle.Attack:Play()
  141.     if PlayDownStab then
  142.         PlayDownStab.Value = true
  143.         wait(1.0)
  144.         PlayDownStab.Value = false
  145.     end
  146.     KnifeUp()
  147. end
  148.  
  149. function ThrowAttack()
  150.     KnifeOut()
  151.     if PlayThrow then
  152.         PlayThrow.Value = true
  153.         wait(0.3)
  154.         if not Handle then return end
  155.         local throwingHandle = Handle:Clone()
  156.         DebrisService:AddItem(throwingHandle, 5)
  157.         throwingHandle.Parent = workspace
  158.         Handle.Throw:Play()
  159.         if MyCharacter and MyHumanoid then
  160.             throwingHandle.Velocity = (MyHumanoid.TargetPoint - throwingHandle.CFrame.p).unit * 300
  161.             -- set the orientation to the direction it is being thrown in
  162.             throwingHandle.CFrame = CFrame.new(throwingHandle.CFrame.p, throwingHandle.CFrame.p + throwingHandle.Velocity) * CFrame.Angles(0, 0, math.rad(-90))
  163.             local floatingForce = Instance.new('BodyForce', throwingHandle)
  164.             floatingForce.force = Vector3.new(0, 196.2 * throwingHandle:GetMass() * 0.98, 0)
  165.             local spin = Instance.new('BodyAngularVelocity', throwingHandle)
  166.             spin.angularvelocity = throwingHandle.CFrame:vectorToWorldSpace(Vector3.new(0, -400, 0))
  167.         end
  168.         Handle.Transparency = 1
  169.         -- Wait so that the knife has left the thrower's general area
  170.         wait(0.08)
  171.         if throwingHandle then
  172.             local touchedConn = throwingHandle.Touched:connect(function(hit) print("hit throw") BlowDamage(hit, THROWING_DAMAGE) end)
  173.         end
  174.         -- must check if it still exists since we waited
  175.         if throwingHandle then
  176.             throwingHandle.CanCollide = true
  177.         end
  178.         wait(0.6)
  179.         if Handle and PlayThrow then
  180.             Handle.Transparency = 0
  181.             PlayThrow.Value = false
  182.         end
  183.     end
  184.     KnifeUp()
  185. end
  186.  
  187. function KnifeUp()
  188.     Tool.GripForward = Vector3.new(0, 0, -1)
  189.     Tool.GripRight = Vector3.new(1, 0, 0)
  190.     Tool.GripUp = Vector3.new(0, 1, 0)
  191. end
  192.  
  193. function KnifeDown()
  194.     Tool.GripForward = Vector3.new(0, 0, -1)
  195.     Tool.GripRight = Vector3.new(1, 0, 0)
  196.     Tool.GripUp = Vector3.new(0, -1, 0)
  197. end
  198.  
  199. function KnifeOut()
  200.     Tool.GripForward = Vector3.new(0, 0, -1)
  201.     Tool.GripRight = Vector3.new(1, 0, 0)
  202.     Tool.GripUp = Vector3.new(0, 1, 0)
  203. end
  204.  
  205. Tool.Enabled = true
  206.  
  207. function OnLeftButtonDown()
  208.     LeftButtonDownTime = time()
  209.     if PlayThrowCharge then
  210.         PlayThrowCharge.Value = true
  211.     end
  212. end
  213.  
  214. function OnLeftButtonUp()
  215.     if not Tool.Enabled then return end
  216.     -- Reset the list of hit players every time we start a new attack
  217.     HitPlayers = {}
  218.     if PlayThrowCharge then
  219.         PlayThrowCharge.Value = false
  220.     end
  221.     if Tool.Enabled and MyHumanoid and MyHumanoid.Health > 0 then
  222.         Tool.Enabled = false
  223.         local currTime = time()
  224.         if LeftButtonDownTime and currTime - LeftButtonDownTime > HOLD_TO_THROW_TIME and
  225.             currTime - LeftButtonDownTime < 1.15 then
  226.             ThrowAttack()
  227.         else
  228.             Attacking = true
  229.             if math.random(1, 2) == 1 then
  230.                 HardAttack()
  231.             else
  232.                 NormalAttack()
  233.             end
  234.             Attacking = false
  235.         end
  236.         Tool.Enabled = true
  237.     end
  238. end
  239.  
  240.  
  241.  
  242. function OnEquipped(mouse)
  243.     PlayStabPunch = WaitForChild(Tool, 'PlayStabPunch')
  244.     PlayDownStab = WaitForChild(Tool, 'PlayDownStab')
  245.     PlayThrow = WaitForChild(Tool, 'PlayThrow')
  246.     PlayThrowCharge = WaitForChild(Tool, 'PlayThrowCharge')
  247. Handle.Equip:Play()
  248.     BlowConnection = Handle.Touched:connect(Blow)
  249.     MyCharacter = Tool.Parent
  250.     MyTorso = MyCharacter:FindFirstChild('Torso')
  251.     MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
  252.     MyPlayer = PlayersService.LocalPlayer
  253.     if mouse then
  254.         Button1DownConnection = mouse.Button1Down:connect(OnLeftButtonDown)
  255.         Button1UpConnection = mouse.Button1Up:connect(OnLeftButtonUp)
  256.     end
  257.     KnifeUp()
  258. end
  259.  
  260. function OnUnequipped()
  261.     -- Unequip logic here
  262.     if BlowConnection then
  263.         BlowConnection:disconnect()
  264.         BlowConnection = nil
  265.     end
  266.     if Button1DownConnection then
  267.         Button1DownConnection:disconnect()
  268.         Button1DownConnection = nil
  269.     end
  270.     if Button1UpConnection then
  271.         Button1UpConnection:disconnect()
  272.         Button1UpConnection = nil
  273.     end
  274.     if Button2UpConnection then
  275.         Button2UpConnection:disconnect()
  276.         Button2UpConnection = nil
  277.     end
  278.     if Button2DownConnection then
  279.         Button2DownConnection:disconnect()
  280.         Button2DownConnection = nil
  281.     end
  282.     MyHumanoid = nil
  283. end
  284.  
  285.  
  286. Tool.Equipped:connect(OnEquipped)
  287. Tool.Unequipped:connect(OnUnequipped)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement