Aquarius_Raverus

Combat System

Nov 8th, 2020 (edited)
2,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.15 KB | None | 0 0
  1. -- Melee Weapon Handler
  2.  
  3. local Storage = game:GetService("ReplicatedStorage")
  4. local Players = game:GetService("Players")
  5. local CP = game:GetService("ContentProvider")
  6.  
  7. local Remotes = Storage:WaitForChild("Remotes")
  8. local Modules = Storage:WaitForChild("Modules")
  9.  
  10. local HitAnims = script:WaitForChild("HitAnims")
  11. local RaycastHitbox = require(Modules.Utils.RaycastHitboxV3)
  12.  
  13. local AllowedActions = {
  14.     ['Equip'] = true,  
  15.     ['Init'] = true,
  16.     ['Attack'] = true,
  17.     ['Block'] = true,
  18.     ['Damage'] = true,
  19. };
  20.  
  21. local EquipCooldowns = {};
  22. local EquippedWeapons = {};
  23.  
  24. local AttackCooldowns = {};
  25. local BlockCooldowns = {};
  26.  
  27. for _,v in pairs(HitAnims:GetChildren()) do
  28.     CP:PreloadAsync({v})
  29. end
  30.  
  31. local function setCooldown(plr, duration)
  32.     EquipCooldowns[plr.UserId] = true
  33.  
  34.     coroutine.resume(coroutine.create(function()
  35.         wait(duration)
  36.         EquipCooldowns[plr.UserId] = nil
  37.     end))
  38. end
  39.  
  40. local function getRandomHitAnim()
  41.     local anims = HitAnims:GetChildren()
  42.     local random = math.random(1, #anims)
  43.     return HitAnims[anims[random].Name]
  44. end
  45.  
  46. Players.PlayerAdded:Connect(function(plr)
  47.     plr.CharacterAdded:Wait()
  48.  
  49.     plr.Character.Humanoid.Died:Connect(function()
  50.         EquippedWeapons[plr.UserId] = nil
  51.     end)
  52. end)
  53.  
  54. Players.PlayerRemoving:Connect(function(plr)
  55.     if EquippedWeapons[plr.UserId] then
  56.         EquippedWeapons[plr.UserId] = nil
  57.     end
  58. end)
  59.  
  60. Remotes.MeleeMain.OnServerInvoke = function(plr, data)
  61.     local Action = data.Action
  62.    
  63.     if Action then
  64.  
  65.         if not AllowedActions[Action] then
  66.             plr:Kick('No exploiting.')
  67.             return;
  68.         end
  69.  
  70.         if Action == 'Equip' then
  71.             if EquipCooldowns[plr.UserId] then
  72.                 return;
  73.             end
  74.  
  75.             local weaponData = data.Data
  76.             local weaponName = weaponData.Name
  77.  
  78.             if plr.Character:FindFirstChild(weaponName.. 'Weld').Transparency == 0 then
  79.                 -- equip
  80.                 plr.Character[weaponName.. 'Weld'].Transparency = 1
  81.  
  82.                 setCooldown(plr, 1)
  83.  
  84.                 local sword = script[weaponName]:Clone()
  85.                 sword.Parent = plr.Character
  86.  
  87.                 EquippedWeapons[plr.UserId] = {
  88.                     Weapon = sword,
  89.                 --  Hitbox = RaycastHitbox:Initialize(sword),
  90.                     Blocking = false,
  91.                     Attacking = false,
  92.                     PrevBlock = tick()
  93.                 }
  94.  
  95.                 local weld = Instance.new("Motor6D", sword)
  96.                 weld.Name = 'Weld'
  97.                 weld.Part0 = sword
  98.                 weld.Part1 = plr.Character['Right Arm']
  99.                 weld.C0 =  CFrame.new(-2.5, 1 ,0) * CFrame.Angles(0, math.rad(-90), 0)
  100.                
  101.                 return 'Equip', EquippedWeapons[plr.UserId].Weapon
  102.             elseif plr.Character[weaponName.. 'Weld'].Transparency == 1 then
  103.                 -- un equip
  104.                 plr.Character[weaponName.. 'Weld'].Transparency = 0
  105.  
  106.                 EquippedWeapons[plr.UserId].Weapon:Destroy()
  107.  
  108.                 if EquippedWeapons[plr.UserId].Blocking == true then
  109.                     -- stop anim
  110.                     EquippedWeapons[plr.UserId].BlockAnim:Stop(0.08)
  111.                 end
  112.  
  113.                 plr.Character.Humanoid.WalkSpeed = 16
  114.  
  115.                 EquippedWeapons[plr.UserId] = nil
  116.  
  117.                 setCooldown(plr, 1)
  118.                
  119.                 return 'Unequip'
  120.             end
  121.  
  122.         end
  123.  
  124.         if Action == 'Init' then
  125.             -- init weld
  126.  
  127.             local weaponData = data.Data
  128.             local sheathePos = weaponData:FindFirstChild('Sheathe_Pos').Value or 'None'
  129.  
  130.             local WeaponClone = script:WaitForChild(weaponData.Name)
  131.             WeaponClone = WeaponClone:Clone()
  132.             WeaponClone.Parent = plr.Character
  133.             WeaponClone.Name = WeaponClone.Name.. 'Weld'
  134.  
  135.             if typeof(sheathePos) == 'number' then
  136.                 -- has sheathe pos
  137.  
  138.                 -- 1 = back | 2 = front
  139.  
  140.                 if sheathePos == 1 then
  141.  
  142.                     local weld = Instance.new("Motor6D", WeaponClone)
  143.                     weld.Name = 'Weld'
  144.                     weld.Part0 = WeaponClone
  145.                     weld.Part1 = plr.Character.Torso
  146.  
  147.                     weld.C0 = CFrame.new(0,0, -0.5) * CFrame.Angles(0, 0, math.rad(45))
  148.  
  149.                 elseif sheathePos == 2 then
  150.  
  151.                     local weld = Instance.new("Motor6D", WeaponClone)
  152.                     weld.Name = 'Weld'
  153.                     weld.Part0 = WeaponClone
  154.                     weld.Part1 = plr.Character.Torso
  155.  
  156.                     weld.C0 = CFrame.new(0,0,0)
  157.  
  158.                 end
  159.             end
  160.  
  161.         end
  162.        
  163.         if Action == 'Damage' then
  164.             if EquippedWeapons[plr.UserId] then
  165.                 if plr.Character:FindFirstChild('Stunned') then
  166.                 --  warn'you are stunned'
  167.                     return;
  168.                 end
  169. --[[
  170.                 if EquippedWeapons[plr.UserId].Attacking == false then
  171.                     print'not attaking'
  172.                     return;
  173.                 end
  174. ]]--           
  175.                 if EquippedWeapons[plr.UserId].Blocking == true then
  176.                     return;
  177.                 end
  178.                
  179.                 local thingHit = data.Hit
  180.                 if not thingHit then return end
  181.                
  182.                 if thingHit:FindFirstChild('Humanoid') then
  183.                     if thingHit.Humanoid.Health <= 0 then return end
  184.                    
  185.                     local targetName = thingHit.Name
  186.                    
  187.                     -- has hum
  188.                     if game.Players:FindFirstChild(targetName) then
  189.  
  190.                     --  print'found player test'
  191.  
  192.                         if EquippedWeapons[game.Players[targetName].UserId] then
  193.  
  194.                             if EquippedWeapons[game.Players[targetName].UserId].Blocking == true then
  195.                                 -- check if behind target
  196.  
  197.                             --  print'other player is blocking'
  198.  
  199.                                 local dot = thingHit.Head.CFrame.LookVector:Dot(plr.Character.Head.CFrame.LookVector)
  200.  
  201.                                 if (dot > 0.5) then
  202.                                     -- is behind
  203.                                     local DamageToTake = data.Damage
  204.                                     thingHit.Humanoid:TakeDamage(DamageToTake)
  205.                                     return;
  206.                                 end
  207.  
  208.                                 -- target is blocking
  209.                                 local hitEffect = script:WaitForChild("Hit_Effect")
  210.                                 local clone = hitEffect:Clone()
  211.                                 clone.Parent = thingHit.HumanoidRootPart
  212.                                 clone.Weld.Part1 = thingHit.Torso                                      
  213.  
  214.                                 clone.Particle:Emit(20)
  215.  
  216.                                 game.Debris:AddItem(clone, 0.5)
  217.  
  218.                                 local chipDamage = data.ChipDmg
  219.                                 thingHit.Humanoid:TakeDamage(chipDamage)
  220.  
  221.                                 return;
  222.  
  223.                             elseif EquippedWeapons[game.Players[targetName].UserId].Blocking == false then
  224.                                 coroutine.resume(coroutine.create(function()
  225.                                     local stunned = Instance.new("BoolValue", thingHit)
  226.                                     stunned.Name = 'Stunned'
  227.                                     stunned.Value = true
  228.                                    
  229.                                     local bv = Instance.new("BodyVelocity", thingHit.PrimaryPart)
  230.                                     bv.Name = 'StunnedBV'
  231.                                     bv.MaxForce = Vector3.new(9999,9999,9999)
  232.                                     bv.Velocity = Vector3.new(0,0,0)
  233.                                    
  234.                                     plr.Character.Humanoid.JumpPower = 0
  235.                                    
  236.                                     local chosenAnim = getRandomHitAnim()
  237.  
  238.                                     local hitAnim = thingHit.Humanoid:LoadAnimation(HitAnims[chosenAnim.Name])
  239.                                     hitAnim:Play()
  240.  
  241.                                     game.Debris:AddItem(stunned, hitAnim.Length)
  242.                                     game.Debris:AddItem(bv, hitAnim.Length)
  243.                                    
  244.                                     coroutine.resume(coroutine.create(function()
  245.                                         wait(hitAnim.Length)
  246.                                         plr.Character.Humanoid.JumpPower = 50
  247.                                     end))
  248.                                 end))
  249.  
  250.                                 local DamageToTake = data.Damage
  251.                                 thingHit.Humanoid:TakeDamage(DamageToTake)
  252.                                 return;
  253.                             end
  254.  
  255.                         else
  256.  
  257.                             coroutine.resume(coroutine.create(function()
  258.                                 local stunned = Instance.new("BoolValue", thingHit)
  259.                                 stunned.Name = 'Stunned'
  260.                                 stunned.Value = true
  261.  
  262.                                 local bv = Instance.new("BodyVelocity", thingHit.PrimaryPart)
  263.                                 bv.Name = 'StunnedBV'
  264.                                 bv.MaxForce = Vector3.new(9999,9999,9999)
  265.                                 bv.Velocity = Vector3.new(0,0,0)
  266.  
  267.                                 plr.Character.Humanoid.JumpPower = 0
  268.  
  269.                                 local chosenAnim = getRandomHitAnim()
  270.  
  271.                                 local hitAnim = thingHit.Humanoid:LoadAnimation(HitAnims[chosenAnim.Name])
  272.                                 hitAnim:Play()
  273.  
  274.                                 game.Debris:AddItem(stunned, hitAnim.Length)
  275.                                 game.Debris:AddItem(bv, hitAnim.Length)
  276.  
  277.                                 coroutine.resume(coroutine.create(function()
  278.                                     wait(hitAnim.Length)
  279.                                     plr.Character.Humanoid.JumpPower = 50
  280.                                 end))
  281.                             end))
  282.  
  283.                             local DamageToTake = data.Damage
  284.                             thingHit.Humanoid:TakeDamage(DamageToTake)
  285.                             return;
  286.                         end
  287.  
  288.                     else
  289.                         local DamageToTake = data.Damage
  290.                         thingHit.Humanoid:TakeDamage(DamageToTake)
  291.                        
  292.                         local chosenAnim = getRandomHitAnim()
  293.                        
  294.                         local hitAnim = thingHit.Humanoid:LoadAnimation(HitAnims[chosenAnim.Name])
  295.                         hitAnim:Play()
  296.                        
  297.                         coroutine.resume(coroutine.create(function()
  298.                             local stunned = Instance.new("BoolValue", thingHit)
  299.                             stunned.Name = 'Stunned'
  300.                             stunned.Value = true
  301.  
  302.                             game.Debris:AddItem(stunned, hitAnim.Length)
  303.                         end))                      
  304.                        
  305.                     end
  306.                    
  307.                 else
  308.                     --print'what?'
  309.                 end
  310.             else
  311.                 return;
  312.             end
  313.         end
  314.     end
  315. end
Advertisement
Add Comment
Please, Sign In to add comment