Advertisement
SxScripting

Sword Combat [Server]

Dec 25th, 2022
4,317
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 1 0
  1. local RS = game:GetService("ReplicatedStorage")
  2. local RunS = game:GetService("RunService")
  3. local Players = game:GetService("Players")
  4.  
  5. local v1 = {}
  6.  
  7. local Animations = {
  8. RS.Animations.Swing1;
  9. RS.Animations.Swing2;
  10. RS.Animations.Swing3;
  11. RS.Animations.Swing4;
  12. RS.Animations.Swing5;
  13. }
  14.  
  15. local Settings = {
  16. Cooldown = .4;
  17. ResetComboTime = 1;
  18. FinalCoolDown = 2;
  19. Damage = 5;
  20. StunDuration = 1;
  21. }
  22.  
  23. local PlayerStats = {}
  24.  
  25. local PlayerFuncs = {}
  26.  
  27. type PlayerData = {--Can ignore, just helps with autocomplete
  28. Player: Player;
  29. Cooldown: boolean;
  30. Timer: RBXScriptConnection?;
  31. lastHit: number;
  32. Combo: number;
  33. SwordHitBox: RBXScriptConnection?;
  34. isStunned: boolean;
  35. Stunned: {PlayerDataMT};
  36. Hits: {Humanoid};
  37. }
  38.  
  39. type PlayerDataMT = typeof(setmetatable({}, { --Can ignore, just helps with autocomplete
  40. __index = PlayerFuncs
  41. })) & PlayerData
  42.  
  43. local function newPlayer(Player)
  44. local PlayerData: PlayerData = {
  45. Player = Player;
  46. Cooldown = false;
  47. Timer = nil;
  48. lastHit = tick();
  49. Combo = 1;
  50. SwordHitBox = nil;
  51. Stunned = {};
  52. Hits = {};
  53. isStunned = false;
  54. }
  55.  
  56. setmetatable(PlayerData, {__index = PlayerFuncs})
  57.  
  58. PlayerStats[Player] = PlayerData
  59. end
  60.  
  61.  
  62. local function removePlayer(Player)
  63. if PlayerStats[Player] then
  64. PlayerStats[Player] = nil
  65. end
  66. end
  67.  
  68. function PlayerFuncs:ApplyParticle(EneChar: Model, TotalWait: number)
  69. local particEffect = game.ServerStorage.HitEffect:Clone()
  70. particEffect.Parent = EneChar:WaitForChild("HumanoidRootPart");
  71. particEffect.CFrame = EneChar:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0,0,-1)
  72. EneChar.Humanoid:LoadAnimation(RS.HitStun):Play()
  73.  
  74. game.Debris:AddItem(particEffect,TotalWait + .7)
  75. end
  76.  
  77. function PlayerFuncs:KnockBack(EnemyCharacter)
  78. local EffectVelocity = Instance.new("BodyVelocity", EnemyCharacter:WaitForChild("HumanoidRootPart"))
  79. EffectVelocity.MaxForce = Vector3.new(1, 1, 1) * 1000000;
  80. EffectVelocity.Velocity = Vector3.new(1, 1, 1) * self.Player.Character:WaitForChild("HumanoidRootPart").CFrame.LookVector * math.random(20, 60)
  81. game:GetService("Debris"):AddItem(EffectVelocity,.3)
  82. end
  83.  
  84. function PlayerFuncs:resetCombo()
  85. self.Combo = 1
  86. end
  87.  
  88. function PlayerFuncs.makeSwordHitBox(self: PlayerDataMT, Sword: Tool,AnimationLength)
  89. if self.SwordHitBox then return end
  90. local OverLapParams = OverlapParams.new()
  91. OverLapParams.FilterType = Enum.RaycastFilterType.Blacklist
  92. OverLapParams.FilterDescendantsInstances = {self.Player.Character}
  93.  
  94. local Parts: {BasePart} = {}
  95.  
  96. for i,v in pairs(Sword:GetDescendants()) do
  97. if v:IsA("BasePart") then
  98. table.insert(Parts, v)
  99. end
  100. end
  101.  
  102. self.SwordHitBox = RunS.Heartbeat:Connect(function()
  103. for i, Part in pairs(Parts) do
  104. local BoundingBox = workspace:GetPartBoundsInBox(Part.CFrame, Part.Size + Vector3.new(0,0,1), OverLapParams)
  105. for i, Hit in pairs(BoundingBox) do
  106. local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
  107. if not Humanoid then continue end
  108. if table.find(self.Hits, Humanoid) then continue end
  109. if self.Combo == (#Animations) then self:KnockBack(Hit.Parent,.3) end;
  110. table.insert(self.Hits, Humanoid)
  111. PlayerFuncs.changeSpeed(Humanoid, 5, 0)
  112. Humanoid:TakeDamage(Settings.Damage)
  113. Humanoid.AutoRotate = false;
  114. self:ApplyParticle(Hit.Parent, AnimationLength)
  115. local HitPlayer = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
  116. if HitPlayer and PlayerStats[HitPlayer] then
  117. local PlayerClass = PlayerStats[HitPlayer]
  118. PlayerClass.isStunned = true
  119. table.insert(self.Stunned, PlayerClass);
  120. end
  121. end
  122. end
  123. end)
  124. end
  125.  
  126. function PlayerFuncs.stopSwordHitBox(self: PlayerDataMT)
  127. if not self.SwordHitBox then return end
  128. self.SwordHitBox:Disconnect()
  129. self.SwordHitBox = nil
  130. end
  131.  
  132. function PlayerFuncs.changeSpeed(Humanoid: Humanoid, WK: number, JP: number)
  133. Humanoid.WalkSpeed = WK;
  134. Humanoid.JumpPower = JP;
  135. end
  136.  
  137. function PlayerFuncs.resetStunned(self: PlayerDataMT)
  138. coroutine.wrap(function()
  139. task.wait(Settings.StunDuration)
  140. for _, v in pairs(self.Stunned) do
  141. v.isStunned = false
  142. end
  143. self.Stunned = {}
  144. end)()
  145. end
  146.  
  147. function PlayerFuncs.resetHits(self: PlayerDataMT)
  148. for _, v in pairs(self.Hits) do
  149. PlayerFuncs.changeSpeed(v, 16, 50)
  150. v.AutoRotate = true
  151. end
  152. self.Hits = {};
  153. end
  154.  
  155. function PlayerFuncs.swing(self: PlayerDataMT, Sword: Tool)
  156. if not self.Player.Character then return end
  157. if self.Cooldown then return end
  158. if self.isStunned then return end
  159. self.Cooldown = true
  160. if self.Combo > #Animations then self:resetCombo() end
  161. if tick() - self.lastHit > Settings.ResetComboTime then self:resetCombo() end
  162. self.lastHit = tick()
  163. local Anim: AnimationTrack = self.Player.Character.Humanoid:LoadAnimation(Animations[self.Combo])
  164. self:makeSwordHitBox(Sword, Anim.Length + .01)
  165. Anim.Priority = Enum.AnimationPriority.Action
  166. Anim:Play()
  167. self.changeSpeed(self.Player.Character:WaitForChild("Humanoid"),5,0)
  168. task.wait(Anim.Length + .01)
  169. self:resetStunned()
  170. self.changeSpeed(self.Player.Character:WaitForChild("Humanoid"),16,50)
  171. self:resetHits()
  172. self.Combo = self.Combo + 1
  173.  
  174. self.lastHit = tick()
  175. self:stopSwordHitBox()
  176.  
  177. if self.Combo == (#Animations + 1) then
  178. self:resetCombo()
  179. task.wait(Settings.FinalCoolDown)
  180. end
  181.  
  182. --wait(Settings.Cooldown)
  183. self.Cooldown = false
  184. end
  185.  
  186. local function onSwingEvent(Player, Sword)
  187. local PlayerData = PlayerStats[Player]
  188. if not PlayerData then return end
  189. PlayerData:swing(Sword)
  190. end
  191.  
  192. RS.Events.Swing.OnServerEvent:Connect(onSwingEvent)
  193. Players.PlayerAdded:Connect(newPlayer)
  194. Players.PlayerRemoving:Connect(removePlayer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement