HowToRoblox

CombatLogHandler

Jul 9th, 2021 (edited)
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.27 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local logDS = dss:GetDataStore("CombatLogData")
  3.  
  4.  
  5.  
  6. game.Players.PlayerAdded:Connect(function(plr)
  7.    
  8.    
  9.     plr.CharacterAdded:Connect(function(char)
  10.        
  11.        
  12.         local humanoid = char:WaitForChild("Humanoid")
  13.        
  14.        
  15.         local data
  16.         pcall(function()
  17.            
  18.             data = logDS:GetAsync(plr.UserId .. "-combat")
  19.         end)
  20.        
  21.        
  22.         if data then
  23.            
  24.             humanoid.Health = data
  25.            
  26.             pcall(function()
  27.                 logDS:RemoveAsync(plr.UserId .. "-combat")
  28.             end)
  29.         end    
  30.        
  31.        
  32.         local oldHealth = humanoid.Health
  33.        
  34.        
  35.         humanoid.HealthChanged:Connect(function(newHealth)
  36.            
  37.            
  38.             if plr:FindFirstChild("InCombat") then
  39.                
  40.                 plr.InCombat.Value = newHealth
  41.             end
  42.            
  43.            
  44.             if newHealth < oldHealth then
  45.                
  46.                 if plr:FindFirstChild("InCombat") then
  47.                     plr.InCombat:Destroy()
  48.                 end
  49.                
  50.                 local value = Instance.new("NumberValue")
  51.                 value.Name = "InCombat"
  52.                
  53.                 value.Parent = plr
  54.                
  55.                 game:GetService("Debris"):AddItem(value, 5)
  56.             end
  57.            
  58.            
  59.             oldHealth = newHealth
  60.         end)
  61.     end)
  62. end)
  63.  
  64.  
  65. game.Players.PlayerRemoving:Connect(function(plr)
  66.    
  67.     if plr:FindFirstChild("InCombat") then
  68.        
  69.        
  70.         pcall(function()
  71.            
  72.             logDS:SetAsync(plr.UserId .. "-combat", plr.InCombat.Value)
  73.         end)
  74.     end
  75. end)
Add Comment
Please, Sign In to add comment