Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RS = game:GetService("ReplicatedStorage")
- local RunS = game:GetService("RunService")
- local Players = game:GetService("Players")
- local v1 = {}
- local Animations = {
- RS.Animations.Swing1;
- RS.Animations.Swing2;
- RS.Animations.Swing3;
- RS.Animations.Swing4;
- RS.Animations.Swing5;
- }
- local Settings = {
- Cooldown = .4;
- ResetComboTime = 1;
- FinalCoolDown = 2;
- Damage = 5;
- StunDuration = 1;
- }
- local PlayerStats = {}
- local PlayerFuncs = {}
- type PlayerData = {--Can ignore, just helps with autocomplete
- Player: Player;
- Cooldown: boolean;
- Timer: RBXScriptConnection?;
- lastHit: number;
- Combo: number;
- SwordHitBox: RBXScriptConnection?;
- isStunned: boolean;
- Stunned: {PlayerDataMT};
- Hits: {Humanoid};
- }
- type PlayerDataMT = typeof(setmetatable({}, { --Can ignore, just helps with autocomplete
- __index = PlayerFuncs
- })) & PlayerData
- local function newPlayer(Player)
- local PlayerData: PlayerData = {
- Player = Player;
- Cooldown = false;
- Timer = nil;
- lastHit = tick();
- Combo = 1;
- SwordHitBox = nil;
- Stunned = {};
- Hits = {};
- isStunned = false;
- }
- setmetatable(PlayerData, {__index = PlayerFuncs})
- PlayerStats[Player] = PlayerData
- end
- local function removePlayer(Player)
- if PlayerStats[Player] then
- PlayerStats[Player] = nil
- end
- end
- function PlayerFuncs:ApplyParticle(EneChar: Model, TotalWait: number)
- local particEffect = game.ServerStorage.HitEffect:Clone()
- particEffect.Parent = EneChar:WaitForChild("HumanoidRootPart");
- particEffect.CFrame = EneChar:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0,0,-1)
- EneChar.Humanoid:LoadAnimation(RS.HitStun):Play()
- game.Debris:AddItem(particEffect,TotalWait + .7)
- end
- function PlayerFuncs:KnockBack(EnemyCharacter)
- local EffectVelocity = Instance.new("BodyVelocity", EnemyCharacter:WaitForChild("HumanoidRootPart"))
- EffectVelocity.MaxForce = Vector3.new(1, 1, 1) * 1000000;
- EffectVelocity.Velocity = Vector3.new(1, 1, 1) * self.Player.Character:WaitForChild("HumanoidRootPart").CFrame.LookVector * math.random(20, 60)
- game:GetService("Debris"):AddItem(EffectVelocity,.3)
- end
- function PlayerFuncs:resetCombo()
- self.Combo = 1
- end
- function PlayerFuncs.makeSwordHitBox(self: PlayerDataMT, Sword: Tool,AnimationLength)
- if self.SwordHitBox then return end
- local OverLapParams = OverlapParams.new()
- OverLapParams.FilterType = Enum.RaycastFilterType.Blacklist
- OverLapParams.FilterDescendantsInstances = {self.Player.Character}
- local Parts: {BasePart} = {}
- for i,v in pairs(Sword:GetDescendants()) do
- if v:IsA("BasePart") then
- table.insert(Parts, v)
- end
- end
- self.SwordHitBox = RunS.Heartbeat:Connect(function()
- for i, Part in pairs(Parts) do
- local BoundingBox = workspace:GetPartBoundsInBox(Part.CFrame, Part.Size + Vector3.new(0,0,1), OverLapParams)
- for i, Hit in pairs(BoundingBox) do
- local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
- if not Humanoid then continue end
- if table.find(self.Hits, Humanoid) then continue end
- if self.Combo == (#Animations) then self:KnockBack(Hit.Parent,.3) end;
- table.insert(self.Hits, Humanoid)
- PlayerFuncs.changeSpeed(Humanoid, 5, 0)
- Humanoid:TakeDamage(Settings.Damage)
- Humanoid.AutoRotate = false;
- self:ApplyParticle(Hit.Parent, AnimationLength)
- local HitPlayer = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
- if HitPlayer and PlayerStats[HitPlayer] then
- local PlayerClass = PlayerStats[HitPlayer]
- PlayerClass.isStunned = true
- table.insert(self.Stunned, PlayerClass);
- end
- end
- end
- end)
- end
- function PlayerFuncs.stopSwordHitBox(self: PlayerDataMT)
- if not self.SwordHitBox then return end
- self.SwordHitBox:Disconnect()
- self.SwordHitBox = nil
- end
- function PlayerFuncs.changeSpeed(Humanoid: Humanoid, WK: number, JP: number)
- Humanoid.WalkSpeed = WK;
- Humanoid.JumpPower = JP;
- end
- function PlayerFuncs.resetStunned(self: PlayerDataMT)
- coroutine.wrap(function()
- task.wait(Settings.StunDuration)
- for _, v in pairs(self.Stunned) do
- v.isStunned = false
- end
- self.Stunned = {}
- end)()
- end
- function PlayerFuncs.resetHits(self: PlayerDataMT)
- for _, v in pairs(self.Hits) do
- PlayerFuncs.changeSpeed(v, 16, 50)
- v.AutoRotate = true
- end
- self.Hits = {};
- end
- function PlayerFuncs.swing(self: PlayerDataMT, Sword: Tool)
- if not self.Player.Character then return end
- if self.Cooldown then return end
- if self.isStunned then return end
- self.Cooldown = true
- if self.Combo > #Animations then self:resetCombo() end
- if tick() - self.lastHit > Settings.ResetComboTime then self:resetCombo() end
- self.lastHit = tick()
- local Anim: AnimationTrack = self.Player.Character.Humanoid:LoadAnimation(Animations[self.Combo])
- self:makeSwordHitBox(Sword, Anim.Length + .01)
- Anim.Priority = Enum.AnimationPriority.Action
- Anim:Play()
- self.changeSpeed(self.Player.Character:WaitForChild("Humanoid"),5,0)
- task.wait(Anim.Length + .01)
- self:resetStunned()
- self.changeSpeed(self.Player.Character:WaitForChild("Humanoid"),16,50)
- self:resetHits()
- self.Combo = self.Combo + 1
- self.lastHit = tick()
- self:stopSwordHitBox()
- if self.Combo == (#Animations + 1) then
- self:resetCombo()
- task.wait(Settings.FinalCoolDown)
- end
- --wait(Settings.Cooldown)
- self.Cooldown = false
- end
- local function onSwingEvent(Player, Sword)
- local PlayerData = PlayerStats[Player]
- if not PlayerData then return end
- PlayerData:swing(Sword)
- end
- RS.Events.Swing.OnServerEvent:Connect(onSwingEvent)
- Players.PlayerAdded:Connect(newPlayer)
- Players.PlayerRemoving:Connect(removePlayer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement