Advertisement
HowToRoblox

OxygenHandler

Feb 27th, 2021
4,331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.92 KB | None | 0 0
  1. local bar = script.Parent.BarBG.Bar
  2. local oxygenLabel = script.Parent.BarBG.OxygenAmount
  3.  
  4. local maxOxygen = 100
  5. local currentOxygen = maxOxygen
  6.  
  7. local timePerLoss = 0.3
  8.  
  9. local isSwimming = false
  10.  
  11. local char = game.Players.LocalPlayer.Character
  12. local hum = char:WaitForChild("Humanoid")
  13.  
  14.  
  15. hum.StateChanged:Connect(function(oldState, newState)
  16.    
  17.    
  18.     if newState == Enum.HumanoidStateType.Swimming then
  19.  
  20.         isSwimming = true
  21.        
  22.     else
  23.         isSwimming = false
  24.     end
  25. end)
  26.  
  27.  
  28. while wait(timePerLoss) do
  29.    
  30.    
  31.     if isSwimming then
  32.        
  33.         currentOxygen = math.clamp(currentOxygen - 1, 0, maxOxygen)
  34.        
  35.     else
  36.         currentOxygen = math.clamp(currentOxygen + 1, 0, maxOxygen)
  37.     end
  38.    
  39.    
  40.     if currentOxygen < 1 then
  41.        
  42.         hum.Health = 0
  43.     end
  44.    
  45.    
  46.     local barScale = currentOxygen / maxOxygen
  47.     bar:TweenSize(UDim2.new(barScale, 0, 1, 0), "InOut", "Linear", timePerLoss)
  48.    
  49.     oxygenLabel.Text = currentOxygen .. " / " .. maxOxygen
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement