Advertisement
HowToRoblox

ShieldScript

Jun 23rd, 2022
925
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local shieldGui = script:WaitForChild("ShieldGui")
  2.  
  3. local shieldMax = 50
  4.  
  5.  
  6. function updateGui(gui, shieldAmount)
  7.    
  8.     gui.ShieldBackground.ShieldHealth.Text = shieldAmount
  9.    
  10.     local y = shieldAmount / shieldMax
  11.    
  12.     gui.ShieldBackground.Clipping.Size = UDim2.new(1, 0, y, 0)
  13.     gui.ShieldBackground.Clipping.ShieldFill.Size = UDim2.new(1, 0, 1/y, 0)
  14. end
  15.  
  16.  
  17. game.Players.PlayerAdded:Connect(function(player)
  18.    
  19.     player.CharacterAdded:Connect(function(character)
  20.        
  21.         local humanoid = character.Humanoid
  22.        
  23.         local newGui = shieldGui:Clone()
  24.         newGui.Parent = character:WaitForChild("Head")
  25.        
  26.         local shield = shieldMax
  27.        
  28.         updateGui(newGui, shield)
  29.        
  30.         local oldHealth = humanoid.Health
  31.        
  32.         humanoid.HealthChanged:Connect(function(newHealth)
  33.            
  34.             if shield > 0 and newHealth < oldHealth then
  35.                
  36.                 humanoid.Health = humanoid.MaxHealth
  37.                
  38.                 local change = oldHealth - newHealth
  39.                
  40.                 shield -= change
  41.                
  42.                 local healthMinus = math.clamp(shield, -humanoid.MaxHealth, 0)
  43.                
  44.                 updateGui(newGui, math.clamp(shield, 0, shieldMax))
  45.            
  46.                 oldHealth = humanoid.MaxHealth + healthMinus
  47.                 humanoid.Health = oldHealth
  48.             end
  49.         end)
  50.     end)
  51. end)
Advertisement
RAW Paste Data Copied
Advertisement