Advertisement
g14ndev

Simple Combat Combo system

Mar 3rd, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.95 KB | Source Code | 0 0
  1. --- local script--
  2.  
  3. local plr = game.Players.LocalPlayer
  4. local UIS = game:GetService("UserInputService")
  5.  
  6. local count = 1
  7. local db = false -- wether a hit can be perfomed or not
  8. local animations = script.Animations
  9. local waitTime = 0.1 -- cooldown between hits
  10. local maxComboTime = 1.5
  11. local lastClickTime = 0
  12. local speedMul = 2.0
  13.  
  14. UIS.InputBegan:Connect(function(input , gpe)
  15.     if gpe then return end
  16.     local characterMove -- initializing animation folder and variables for said animations
  17.     local hit1
  18.     local hit2
  19.     local hit3
  20.     local hit4
  21.  
  22.  
  23.     if input.UserInputType == Enum.UserInputType.MouseButton1 and plr.Character:GetAttribute("Stun") == false and plr.Character:GetAttribute("IFrame") == false then
  24.         local currentTime = tick()
  25.         local timeSinceLastClick = currentTime - lastClickTime
  26.         lastClickTime = currentTime
  27.         if plr.state["type"].Value == 1 then -- checks if the users "type" is 1 ("refrencing a specific character")
  28.             characterMove = animations.AlastorAnimations
  29.             hit1 = characterMove["1"]  -- asign each hit to a specific animation in the characterMove folder
  30.             hit2 = characterMove["2"]
  31.             hit3 = characterMove["1"]
  32.             hit4 = characterMove["2"]
  33.            
  34.         end
  35.         if db == true then return end -- if db is true then we cannot click
  36.         if timeSinceLastClick > maxComboTime then
  37.             count = 1
  38.         end
  39.        
  40.         if count == 1 and not db then
  41.             local targetCount = 2
  42.            
  43.            
  44.             local humanoid = plr.Character:WaitForChild("Humanoid")
  45.             local animator = humanoid:WaitForChild("Animator")
  46.             local animationTrack = animator:LoadAnimation(hit1)
  47.            
  48.            
  49.             animationTrack:Play()
  50.             animationTrack:AdjustSpeed(speedMul)
  51.             animationTrack.KeyframeReached:Connect(function(keyframeName)
  52.                
  53.                 if keyframeName == "Damage" then
  54.                     game.ReplicatedStorage.CombatRemote:FireServer(count -1)
  55.                 else
  56.                     return
  57.                 end
  58.                
  59.             end)
  60.             count = targetCount
  61.             db = true
  62.             task.wait(waitTime) -- setting db to true then waiting to set it to false
  63.             db = false
  64.  
  65.        
  66.         elseif count == 2 and not db then
  67.             local targetCount = 3
  68.            
  69.             local humanoid = plr.Character:WaitForChild("Humanoid")
  70.             local animator = humanoid:WaitForChild("Animator")
  71.             local animationTrack = animator:LoadAnimation(hit2)
  72.            
  73.             animationTrack:Play()
  74.             animationTrack:AdjustSpeed(speedMul)
  75.             animationTrack.KeyframeReached:Connect(function(keyframeName)
  76.  
  77.                 if keyframeName == "Damage" then
  78.                     game.ReplicatedStorage.CombatRemote:FireServer(count -1)
  79.  
  80.                 end
  81.  
  82.             end)
  83.             count = targetCount
  84.             db = true
  85.             task.wait(waitTime)
  86.             db = false
  87.            
  88.  
  89.        
  90.         elseif count == 3 and not db then
  91.             local targetCount = 4
  92.            
  93.            
  94.             local humanoid = plr.Character:WaitForChild("Humanoid")
  95.             local animator = humanoid:WaitForChild("Animator")
  96.             local animationTrack = animator:LoadAnimation(hit3)
  97.            
  98.             animationTrack:Play()
  99.             animationTrack:AdjustSpeed(speedMul)
  100.             animationTrack.KeyframeReached:Connect(function(keyframeName)
  101.  
  102.                 if keyframeName == "Damage" then
  103.                     game.ReplicatedStorage.CombatRemote:FireServer(count -1)
  104.  
  105.                 end
  106.  
  107.             end)
  108.             count = targetCount
  109.             db = true
  110.             task.wait(waitTime)
  111.             db = false
  112.  
  113.        
  114.        
  115.         elseif count > 3 and not db then
  116.            
  117.            
  118.             local humanoid = plr.Character:WaitForChild("Humanoid")
  119.             local animator = humanoid:WaitForChild("Animator")
  120.             local animationTrack = animator:LoadAnimation(hit4)
  121.            
  122.             animationTrack:Play()
  123.             animationTrack:AdjustSpeed(speedMul)
  124.             animationTrack.KeyframeReached:Connect(function(keyframeName)
  125.  
  126.                 if keyframeName == "Damage" then
  127.                     game.ReplicatedStorage.CombatRemote:FireServer(4)
  128.  
  129.                 end
  130.  
  131.             end)
  132.             count = 1
  133.             db = true
  134.            
  135.             task.wait(maxComboTime)
  136.            
  137.             db = false
  138.            
  139.         end
  140.     end
  141. end)
  142.  
  143. ---- server script ---
  144.  
  145.  
  146. local debris = game:GetService("Debris")
  147. local damage = 1
  148. local stunTime = 0.8
  149. local Modules = game:GetService("ServerStorage").Modules
  150. local RagdollModule = require(Modules.RagDollModule)
  151. local ragTime = 2
  152. local reTime = 5
  153. local userInputService = game:GetService("UserInputService")
  154. game.ReplicatedStorage.CombatRemote.OnServerEvent:Connect(function(player, count)
  155.     local TBL = {player.Name,count}
  156.    
  157.     local miss = game.ServerStorage.Sounds["Punch Swing "]:Clone()
  158.    
  159.     miss.Parent = player.Character
  160.     miss:Play()
  161.     debris:AddItem(miss,3)
  162.    
  163.    
  164.     local character = player.Character
  165.    
  166.     character:SetAttribute("Attacking", true)
  167.     if character then
  168.         local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
  169.        
  170.         -- Create the damage box
  171.         local damageBox = Instance.new("Part")
  172.        
  173.         damageBox.CanTouch = true
  174.         damageBox.Size = Vector3.new(5, 6, 5) -- Adjust the size as needed
  175.         damageBox.Anchored = false
  176.         damageBox.CanCollide = false
  177.         damageBox.Transparency = 0.5
  178.         damageBox.BrickColor = BrickColor.new("Bright red")
  179.        
  180.         damageBox.Name = "DamageBox"
  181.         damageBox.Parent = game.Workspace
  182.         damageBox.CFrame = humanoidRootPart.CFrame * CFrame.new(0,0,-2.5) -- Adjust the distance in front of the player      
  183.        
  184.        
  185.         damageBox.Touched:Connect(function(target)
  186.             if target == nil then return end
  187.            
  188.             if target.Name == "Block" then
  189.                 character:SetAttribute("Stun", true)
  190.                
  191.                 wait(2)
  192.                
  193.                 character:SetAttribute("Stun",false)
  194.                
  195.                 return
  196.             end
  197.             if target.Parent == nil then return end
  198.            
  199.             local hum = target.Parent:FindFirstChild("Humanoid")
  200.            
  201.             if hum and count <= 3 and target.Parent:GetAttribute("IFrame") == false and target.Parent:GetAttribute("Ragdoll") == false then
  202.    
  203.                 if target.Parent ~= player.Character then
  204.                     local punch = game.ServerStorage.Sounds:WaitForChild("HitPunch")
  205.                     local Pclone = punch:Clone()
  206.  
  207.                     Pclone.Parent = player.Character
  208.                     Pclone:Play()
  209.                     debris:AddItem(Pclone,3)
  210.                     if hum.Health - damage <= 0  and hum.Health > 0 then
  211.                         hum:TakeDamage(damage)
  212.                         player.leaderstats.kills.Value +=1
  213.                         RagdollModule.RagdollCharacter(target.Parent)
  214.                         RagdollModule.knockBack(target.Parent, character:WaitForChild("HumanoidRootPart"))
  215.                         task.wait(reTime)
  216.                         RagdollModule.Unragdoll(target.Parent)
  217.                         return
  218.                     end
  219.                     hum:TakeDamage(damage)
  220.                    
  221.                     target.Parent:SetAttribute("Stun", true)
  222.                     task.wait(stunTime)
  223.                     target.Parent:SetAttribute("Stun", false)
  224.                    
  225.                 end
  226.             elseif hum and count > 3 and target.Parent:GetAttribute("IFrame") == false and target.Parent:GetAttribute("Ragdoll") == false then
  227.                
  228.                 if target.Parent ~= player.Character then
  229.                     local punch = game.ServerStorage.Sounds:WaitForChild("HitPunch")
  230.                     local Pclone = punch:Clone()
  231.  
  232.                     Pclone.Parent = player.Character
  233.                     Pclone:Play()
  234.                     debris:AddItem(Pclone,3)
  235.                     if hum.Health - damage <= 0  and hum.Health > 0 then
  236.                         hum:TakeDamage(damage)
  237.                         player.leaderstats.kills.Value +=1
  238.                         RagdollModule.RagdollCharacter(target.Parent)
  239.                         RagdollModule.knockBack(target.Parent, character:WaitForChild("HumanoidRootPart"))
  240.                         task.wait(reTime)
  241.                         RagdollModule.Unragdoll(target.Parent)
  242.                         return
  243.                     end
  244.                     hum:TakeDamage(damage)
  245.                    
  246.                    
  247.                     target.Parent:SetAttribute("Stun", true)
  248.                     RagdollModule.RagdollCharacter(target.Parent)
  249.                     RagdollModule.knockBack(target.Parent, character:WaitForChild("HumanoidRootPart"))
  250.                     task.wait(1)
  251.                     target.Parent:SetAttribute("Stun", false)
  252.                     task.wait(ragTime -1)
  253.                     RagdollModule.Unragdoll(target.Parent)
  254.                    
  255.  
  256.                 end
  257.             else
  258.                 return
  259.            
  260.             end
  261.            
  262.            
  263.            
  264.         end)
  265.         debris:AddItem(damageBox,0.1)
  266.        
  267.     else
  268.         return
  269.     end
  270.     wait(0.5)
  271.     character:SetAttribute("Attacking", false)
  272.    
  273.    
  274.    
  275.    
  276. end)
  277.  
  278.  
  279. --- ragdoll module ---
  280.  
  281.  
  282.  
  283. local RagdollModule = {}
  284.  
  285.  
  286. function RagdollModule.knockBack(target, attackerHRP)
  287.     local targetHRP = target:WaitForChild("HumanoidRootPart")
  288.  
  289.     -- Calculate the direction from the attacker's HRP to the target's HRP
  290.     local direction = (targetHRP.Position - attackerHRP.Position).Unit
  291.  
  292.     -- Apply a force to the target's HRP in the calculated direction
  293.     local knockbackForce = Instance.new("BodyVelocity")
  294.     knockbackForce.Velocity = direction * 35 -- Adjust the force as needed
  295.     knockbackForce.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
  296.     knockbackForce.Parent = targetHRP
  297.  
  298.     -- Apply an upward force to put the target in the air
  299.     local jumpForce = Instance.new("BodyVelocity")
  300.     jumpForce.Velocity = Vector3.new(0, 1000, 0) -- Adjust the upward force as needed
  301.     jumpForce.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
  302.     jumpForce.Parent = targetHRP
  303.  
  304.     -- Remove the knockback and jump forces after a short delay
  305.     wait(0.5) -- Adjust the delay as needed
  306.     knockbackForce:Destroy()
  307.     jumpForce:Destroy()
  308. end
  309.  
  310. function RagdollModule.RagdollCharacter(target)
  311.     if target:GetAttribute("Ragdoll") == true then return end
  312.     target:SetAttribute("Ragdoll", true)
  313.     target:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.Physics)
  314.    
  315.    
  316.     -- Check if the target has a Humanoid
  317.     local humanoid = target:FindFirstChildOfClass("Humanoid")
  318.     if not humanoid then
  319.         return
  320.     end
  321.  
  322.     for i, joint in pairs(target:GetDescendants())do
  323.         if joint:IsA('Motor6D') then
  324.             local socket = Instance.new("BallSocketConstraint", joint.Parent)
  325.             local att0 = Instance.new('Attachment', joint.Part0)
  326.             local att1 = Instance.new('Attachment', joint.Part1)
  327.            
  328.             att0.CFrame = joint.C0
  329.             att1.CFrame = joint.C1
  330.            
  331.             socket.Attachment0 = att0
  332.             socket.Attachment1 = att1
  333.            
  334.             socket.TwistLimitsEnabled = true
  335.             socket.LimitsEnabled = true
  336.            
  337.            
  338.             joint.Enabled = false
  339.         end
  340.     end
  341.    
  342.  
  343. end
  344.  
  345. function RagdollModule.Unragdoll(target)
  346.     if target == nil then return end
  347.     target:SetAttribute("Stun", false)
  348.    
  349.    
  350.     target:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.GettingUp)
  351.  
  352.     for i, joint in pairs(target:GetDescendants())do
  353.         if joint:IsA("Motor6D") then
  354.  
  355.  
  356.             joint.Enabled = true
  357.         elseif joint:IsA("BallSocketConstraint") then
  358.             joint.Enabled = false
  359.            
  360.            
  361.         end
  362.     end
  363.  
  364.    
  365.     target:SetAttribute("Ragdoll", false)
  366. end
  367.  
  368. return RagdollModule
  369.  
  370. -- end ---
  371.  
Tags: Roblox lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement