Advertisement
TaylorsRus

Untitled

Apr 7th, 2024
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.00 KB | None | 0 0
  1. local ResourceManager = {}
  2.  
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local TweenService = game:GetService("TweenService")
  5. local SoundService = game:GetService("SoundService")
  6. local Lighting = game:GetService("Lighting")
  7. local Player = game:GetService("Players").LocalPlayer
  8.  
  9. local Config = ReplicatedStorage:WaitForChild("Config")
  10. local PlayerGui = Player:WaitForChild("PlayerGui")
  11.  
  12. local CharacterData = require(Config:WaitForChild("Character"))
  13.  
  14. local DepthOfField = Lighting:WaitForChild("DepthOfField")
  15. local Blur = Lighting:WaitForChild("Blur")
  16.  
  17. local MainGui = PlayerGui:WaitForChild("MainGui")
  18. local MainContainer = MainGui:WaitForChild("MainContainer")
  19. local HealthBar = MainContainer:WaitForChild("Health"):WaitForChild("HealthBar")
  20. local StaminaBar = MainContainer:WaitForChild("Stamina"):WaitForChild("StaminaBar")
  21.  
  22. local REGEN_RATE = 2
  23. local REGEN_AMOUNT = 1
  24. local REGEN_DELAY = 15
  25.  
  26. local HealthUpdated, StaminaUpdated = nil
  27.  
  28. local CanRegen = true
  29. local LastHurt = nil
  30.  
  31. local CachedValues = {
  32.     Health = {
  33.         Previous = CharacterData.Health.Max,
  34.         Effects = {}
  35.     },
  36.     Stamina = {
  37.         Previous = CharacterData.Stamina.Max,
  38.         Effects = {}
  39.     }
  40. }
  41.  
  42. local BlurOn = TweenService:Create(Blur, TweenInfo.new(1), {Size = 10})
  43. local BlurOff = TweenService:Create(Blur, TweenInfo.new(1.5), {Size = 0})
  44.  
  45. local function ResourceEffects(Resource, Toggle)
  46.     local Character = Player.Character
  47.     local ResourceData = CharacterData[Resource]
  48.     local ResourceValues = CachedValues[Resource]
  49.    
  50.     local EffectData = ResourceData.Effects
  51.     local EffectValues = ResourceValues.Effects
  52.        
  53.     local CurrentResource = Character:GetAttribute(Resource)
  54.     local MaxResource = ResourceData.Max
  55.     local PercentResource = CurrentResource / MaxResource * 100
  56.    
  57.     for Percent, Effect in EffectData do
  58.         if PercentResource < Percent and not table.find(EffectValues, Effect) then
  59.             EffectValues[Percent] = Effect
  60.         elseif PercentResource > Percent then
  61.             EffectValues[Percent] = nil
  62.         end
  63.     end
  64.  
  65.     for Percent, Effect in EffectData do
  66.         print(Effect)
  67.         local Exists = nil
  68.         if EffectValues[Percent] then
  69.             Exists = true
  70.         else
  71.             Exists = false
  72.         end
  73.  
  74.         ResourceManager[Effect](Exists)
  75.     end
  76. end
  77.  
  78. local function UpdateResourceBar(Resource)
  79.     local ResourceBar = Resource == "Health" and HealthBar
  80.         or Resource == "Stamina" and StaminaBar
  81.     local UIGradient = ResourceBar:FindFirstChildOfClass("UIGradient")
  82.    
  83.     local Character = Player.Character
  84.     local ResourceData = CharacterData[Resource]
  85.     local ResourceValue = Character:GetAttribute(Resource)
  86.    
  87.     local MaxOfResource = ResourceData.Max
  88.     local DecimalOfResource = (ResourceValue / MaxOfResource)
  89.     if Resource == "Stamina" then
  90.         --// This is because the stamina bar is upside down, blame past devs.
  91.         DecimalOfResource = -DecimalOfResource
  92.     end
  93.    
  94.     local TweenBar = TweenService:Create(UIGradient,
  95.         TweenInfo.new(.4), {Offset = Vector2.new(DecimalOfResource, 0)})
  96.     TweenBar:Play()
  97. end
  98.  
  99. local function UpdateResource(Resource)
  100.     local Character = Player.Character
  101.     local Humanoid = Character.Humanoid
  102.    
  103.     local ResourceData = CharacterData[Resource]
  104.    
  105.     local CurrentResource = Character:GetAttribute(Resource)
  106.     local PreviousResource = CachedValues[Resource].Previous   
  107.    
  108.     local MaxResource = ResourceData.Max
  109.    
  110.     if CurrentResource < PreviousResource then
  111.         LastHurt = os.clock()
  112.        
  113.         task.spawn(function()
  114.             if CanRegen == false then
  115.                 return
  116.             end
  117.             CanRegen = false
  118.  
  119.             local RegenTimer = os.clock()
  120.             repeat task.wait()
  121.                 RegenTimer = os.clock()
  122.             until RegenTimer - LastHurt > REGEN_DELAY
  123.            
  124.             CanRegen = true
  125.         end)
  126.     end
  127.    
  128.     CachedValues[Resource].Previous = CurrentResource
  129.     UpdateResourceBar(Resource)
  130.     ResourceEffects(Resource)
  131.  
  132.     if CanRegen == nil or CurrentResource == MaxResource then
  133.         return
  134.     end
  135.     if CanRegen == false then
  136.         repeat task.wait(REGEN_RATE) until CanRegen == true
  137.     end
  138.    
  139.     CanRegen = nil 
  140.     task.spawn(function()
  141.         while Character:GetAttribute(Resource) < MaxResource do task.wait(REGEN_RATE)
  142.             if CanRegen == false then
  143.                 break
  144.             end
  145.            
  146.             local CurrentResource = Character:GetAttribute(Resource)
  147.             local NewResource = CurrentResource + REGEN_AMOUNT
  148.        
  149.             if NewResource > MaxResource then
  150.                 NewResource = MaxResource
  151.             end
  152.            
  153.             Character:SetAttribute(Resource, NewResource)
  154.         end
  155.     end)
  156. end
  157.  
  158.  
  159. local function OnCharacterAdded(Character)
  160.     local Humanoid = Character:WaitForChild("Humanoid")
  161.    
  162.     HealthUpdated = Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
  163.         Character:SetAttribute("Health", Humanoid.Health)
  164.     end)
  165.     Character:GetAttributeChangedSignal("Health"):Connect(function()
  166.         UpdateResource("Health")
  167.     end)
  168.     Character:GetAttributeChangedSignal("Stamina"):Connect(function()
  169.         UpdateResource("Stamina")
  170.     end)
  171.    
  172.     Character:SetAttribute("Health", Humanoid.Health)
  173.     Humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
  174. end
  175.  
  176. function ResourceManager.Init()
  177.     if Player.Character then
  178.         OnCharacterAdded(Player.Character)
  179.     end
  180.     Player.CharacterAdded:Connect(OnCharacterAdded)
  181. end
  182.  
  183. function ResourceManager.Tired(Toggle)
  184.     print("Tired")
  185.     local Sound = SoundService:FindFirstChild("Breathing")
  186.     if not Toggle then
  187.         if Sound then
  188.             Sound:Destroy()
  189.         end
  190.         return
  191.     end
  192.    
  193.     if not Sound then
  194.         Sound = _G.GetSound("Breathing")
  195.         Sound.PlayOnRemove = false
  196.         Sound.Looped = true
  197.         Sound.Volume = .2
  198.         _G.PlaySoundLocally(Sound)
  199.     end
  200. end
  201.  
  202. function ResourceManager.Fatigued(Toggle)
  203.     local Tween = Toggle and BlurOn or BlurOff
  204.     if Blur.Enabled and Toggle then
  205.         return
  206.     end
  207.    
  208.     Tween:Play()
  209.     if not Toggle then
  210.         Tween.Completed:Wait()
  211.     end
  212.     Blur.Enabled = Toggle
  213. end
  214.  
  215. function ResourceManager.Exhausted(Toggle)
  216.     print(`{Player.Name} now exhausted.`)
  217. end
  218.  
  219. function ResourceManager.Hurt(Toggle)
  220.     local Heartbeat = SoundService:FindFirstChild("Heartbeat")
  221.     if not Toggle then
  222.         if Heartbeat then
  223.             Heartbeat:Destroy()
  224.         end
  225.         return
  226.     end
  227.        
  228.     if not Heartbeat then
  229.         Heartbeat = _G.GetSound("Heartbeat")
  230.         Heartbeat.PlayOnRemove = false
  231.         Heartbeat.Looped = true
  232.         Heartbeat.Volume = .2
  233.         _G.PlaySoundLocally(Heartbeat)
  234.     end
  235.    
  236.     Heartbeat.PlaybackSpeed = 1
  237. end
  238.  
  239. function ResourceManager.Damaged(Toggle)
  240.     local Tinnitus = SoundService:FindFirstChild("Tinnitus")
  241.     if not Toggle then
  242.         if Tinnitus then
  243.             Tinnitus:Destroy()
  244.         end
  245.         return
  246.     end
  247.    
  248.     local Heartbeat = SoundService:FindFirstChild("Heartbeat")
  249.    
  250.     if not Tinnitus then
  251.         Tinnitus = _G.GetSound("Tinnitus")
  252.    
  253.         Tinnitus.PlayOnRemove = false
  254.         Tinnitus.Looped = true
  255.         Tinnitus.Volume = .2
  256.         _G.PlaySoundLocally(Tinnitus)
  257.     end
  258.    
  259.     if Toggle and Heartbeat.PlaybackSpeed < 1.2 then
  260.         Heartbeat.PlaybackSpeed = 1.2
  261.     end
  262. end
  263.  
  264. function ResourceManager.Dying(Toggle)
  265.     local Heartbeat = SoundService:FindFirstChild("Heartbeat")
  266.     if not Toggle then
  267.         return
  268.     end
  269.    
  270.     print(Heartbeat:GetFullName())
  271.    
  272.     if Toggle and Heartbeat.PlayBackSpeed < 1.5 then
  273.         Heartbeat.PlaybackSpeed = 1.5
  274.     end
  275.     print(`{Player.Name} now dying.`)
  276. end
  277.  
  278. return ResourceManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement