Advertisement
extreime

Code Example

May 6th, 2024
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.95 KB | Source Code | 0 0
  1. --//  Client // --
  2.  
  3. -- Services --
  4. local replicatedStorage = game:GetService("ReplicatedStorage")
  5. local userInputService = game:GetService("UserInputService")
  6. local players = game:GetService("Players")
  7.  
  8. -- Modules --
  9. local modules = replicatedStorage:WaitForChild("Modules")
  10.  
  11. local globalModules = require(modules.globalModules)
  12. local clientModules = require(modules.clientModules)
  13.  
  14. local animationService = require(modules.animationService)
  15.  
  16. local hitboxService = require(globalModules.hitboxService)
  17.  
  18.  -- Some variables --
  19. local player = players.LocalPlayer
  20. local character = player.Character
  21. local humanoid = character.Humanoid
  22. local rootPart = character.HumanoidRootPart
  23.  
  24. local events = replicatedStorage:WaitForChild("Communication").Events
  25. local functions = replicatedStorage:WaitForChild("Communication").Functions
  26.  
  27. local animations = animationService:LoadAnimations(character).animations
  28.  
  29. local combatEvent = events.combat
  30. local cooldownEvent = events.cooldown
  31.  
  32. -- combat system variables --
  33. local currentTrack = nil
  34. local resetCooldown = 1
  35. local comboDelay = 1.5
  36. local attackCooldown = 0.45
  37.  
  38. local combo = 1
  39. local maxCombo = 5
  40. local canAttack = true
  41. local canBlock = true
  42.  
  43. userInputService.InputBegan:Connect(function(input, typing)
  44.     if typing then -- if the player is writing in the chat, the code will ignore his input
  45.         return
  46.     end
  47.    
  48.     if input.UserInputType == Enum.UserInputType.MouseButton1 then
  49.         if not character:GetAttribute("blocking") and not character:GetAttribute("comboCooldown") then -- running the block of code if player isn't blocking or isn't on comboCooldown
  50.             if canAttack then -- running the part of code that gives life to the combatSystem(if canAttack is true)
  51.                 canAttack = false
  52.                 canBlock = false
  53.                
  54.                 character:SetAttribute("running", false) -- changing the running attribute to prevent player from running while attacking
  55.                 -- different attack types depending on player key and attribute state
  56.                 local airCombo = (userInputService:IsKeyDown(Enum.KeyCode.Space) and not character:GetAttribute("aerialCombo") and combo == 4) or false
  57.                 local downSlam = (combo == 5 and character:GetAttribute("aerialCombo"))
  58.                 -- getting the respective animationTrack relative to the attackType
  59.                 local track = airCombo and animations.launcher or downSlam and animations.downSlam
  60.                     or animations[tostring(combo)]
  61.                
  62.                 if not track then
  63.                     warn("track does not exist")
  64.                        
  65.                     return -- exiting the function if the track doesn't exists
  66.                 end
  67.                
  68.                 humanoid.JumpPower = 0 -- preventing player from jumping for better immersive combat
  69.                
  70.                 task.delay(track.Length + 0.45, function()
  71.                     if currentTrack == track then
  72.                         humanoid.JumpPower = 50
  73.                     end
  74.                 end) -- enabling jumppower after short cooldown
  75.                
  76.                 -- stoping previus animation(if its looped)
  77.                 if currentTrack then
  78.                     if currentTrack.IsPlaying then
  79.                         currentTrack:Stop()
  80.                     end
  81.                 end
  82.                
  83.                 local currentCombo = combo
  84.                  -- playing the currentAnimation obtained previously in the track variable
  85.                 currentTrack = track
  86.                 currentTrack:Play()
  87.                
  88.                 track:GetMarkerReachedSignal("ConnectionPoint"):Once(function()-- using animationSignalMarker to run some hitbox and serverTrigger scripts
  89.                     local foundTargets = hitboxService:spatialQuery(character, track.Name == "launcher" and "single" or "multi")
  90.                    
  91.                     --print(currentCombo .. ":",foundTargets)
  92.  
  93.                     combatEvent:FireServer("registerHit", {foundTargets, currentCombo, airCombo})
  94.                 end)
  95.                  -- reseting attack and block once the attack has ended
  96.                 track:GetMarkerReachedSignal("AttackEnded"):Once(function()
  97.                     canAttack = true
  98.                     canBlock = true
  99.                 end)
  100.                 -- creating a server sided cooldown and creating a soundEffect
  101.                 cooldownEvent:FireServer({["attacking"] = track.Length + 0.35, ["noJump"] = track.Length + 0.45})
  102.                 combatEvent:FireServer("soundEffects", {airCombo})
  103.                
  104.                 -- creating the logic of the combat algorithm
  105.                 if combo >= 5 then
  106.                    
  107.                     canAttack = false
  108.                     -- reseting the combo once the combo has ended
  109.                     task.delay(comboDelay, function()
  110.                         combo = 1
  111.                         canAttack = true
  112.                         humanoid.JumpPower = 50
  113.                     end)
  114.                 else
  115.                     local old = combo
  116.                     --reseting the combo if the player takes too long to attack
  117.                     task.delay(resetCooldown, function()
  118.                         if combo == old + 1 then
  119.                             combo = 1
  120.                            
  121.                             canAttack = true
  122.                         end
  123.                     end)
  124.                 end
  125.                
  126.                 combo += 1
  127.             end
  128.         end
  129.     end
  130. end)
  131. -- another mechanism to reset the combo
  132. combatEvent.OnClientEvent:Connect(function(request)
  133.     if request == "resetCombo" then
  134.         combo = 1
  135.     end
  136. end)
  137.  
  138. ---------- // Server // ----
  139.  
  140. -- Services --
  141. local replicatedStorage = game:GetService("ReplicatedStorage")
  142. local players = game:GetService("Players")
  143. -- Modules --
  144. local modules = replicatedStorage:WaitForChild("Modules")
  145.  
  146. local globalModules = require(modules.globalModules)
  147. local cooldownService = require(modules.cooldownService)
  148.  
  149. local hitboxService = require(globalModules.hitboxService)
  150.  -- Events --
  151. local Events = replicatedStorage:WaitForChild("Communication").Events
  152. local Functions = replicatedStorage:WaitForChild("Communication").Functions
  153.  
  154. local combatEvent = Events.combat
  155. local cooldownEvent = Events.cooldown
  156.  
  157. local information = require(modules.information)
  158.  -- some combat Functions that affects the gameplay
  159. local COMBAT_FUNCTIONS = {
  160.     registerHit = function(player, combatInformations, foundTargets, currentCombo, airCombo)
  161.         if not cooldownService:getActiveCooldowns(player, {"comboCooldown", "block"}) then -- running the code if "comboCooldown" and "block" isnt on cooldown
  162.             if cooldownService:getActiveCooldowns(player, "soundPlayed") then -- creating a newSound cooldown if it doesnt exists
  163.                 cooldownService:setCooldown(player, "soundPlayed", false)
  164.             end
  165.              -- changing combatInformations elements based on combo value/ creating a new cooldown when the combo is finished
  166.             if currentCombo == 5 then
  167.                 combatInformations.knockback = true
  168.                 combatInformations.canBlockBreak = true
  169.                
  170.                 cooldownService:setCooldown(player, "comboCooldown"):extend(1.5)
  171.             end
  172.             -- changing the aerial element if aerial is true
  173.             if airCombo then
  174.                 combatInformations.aerial = true
  175.             end
  176.            
  177.             -- running hitboxService function to start the algoritm that checks if the damage is legal
  178.             hitboxService:registerDamage(foundTargets, player, currentCombo, airCombo, combatInformations)
  179.         end
  180.     end,
  181.    
  182.     block = function(player, combatInformations, active) -- server side block functionabilities
  183.         local character = player.Character
  184.         local rootPart = character:FindFirstChild("HumanoidRootPart")
  185.         local humanoid = character:FindFirstChild("Humanoid")
  186.        
  187.         if not rootPart or not humanoid then
  188.             return
  189.         end
  190.         -- part of code that checks if the player has the attribute block as true and, if the player does, creates a intConstrained value that i personaly think that works good in this situation.
  191.         if not character:GetAttribute("block") then
  192.             if not cooldownService:getActiveCooldowns(player, {"block", "blockCooldown",  "stunned"}) then
  193.                 print("Blocking")
  194.                
  195.                 local blockBarValue = Instance.new("IntConstrainedValue")
  196.                 blockBarValue.MaxValue = 5
  197.                 blockBarValue.Value = 5
  198.                 blockBarValue.Name = "blockBar"
  199.                 blockBarValue.Parent = character
  200.                 -- creating a new cooldown called block. Toggle cooldown type
  201.                 cooldownService:setCooldown(character, "block")
  202.             end
  203.         else -- part of the code that runs if the player is currently blocking
  204.             print("stopped blocking")
  205.            
  206.             local blockBarValue = character:FindFirstChild("blockBarValue")
  207.            
  208.             if blockBarValue then
  209.                 blockBarValue:Destroy()
  210.             end
  211.              -- removing block cooldown, with a delay of 1 second
  212.             cooldownService:setCooldown(character, {["block"] = false, ["blockCooldown"] = 1})
  213.         end
  214.     end,
  215.    
  216.     soundEffects = function(player) -- creating sound effects with cooldown creating
  217.         local character = player.Character
  218.        
  219.         if not cooldownService:getActiveCooldowns(player, {"comboCooldown", "soundPlayed", "block"}) then
  220.             local swingSound = replicatedStorage.Sounds.Combat.swing:Clone()
  221.  
  222.             swingSound.Parent = character.HumanoidRootPart
  223.             swingSound:Play()
  224.            
  225.             cooldownService:setCooldown(player, "soundPlayed"):extend(0.5, false)
  226.         end
  227.     end,
  228. }
  229.  
  230. combatEvent.OnServerEvent:Connect(function(player, actionName, arguments) -- listening and calling the function of the COMBAT_FUNCTIONS with the respective key
  231.     local character = player.Character
  232.    
  233.     --print("action: " .. actionName)
  234.     local combatInformations = information("combat")
  235.     COMBAT_FUNCTIONS[actionName](player, combatInformations, unpack(arguments))
  236. end)
  237.  
  238. -- creating a cooldown and reseting after length(in seconds) (similar to Debris, but with cooldowns)
  239. cooldownEvent.OnServerEvent:Connect(function(player, arguments)
  240.     for name, length in next, arguments do
  241.         cooldownService:setCooldown(player, name):extend(length, false)
  242.     end
  243. end)
  244.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement