Oxygen bar which regenerates with the ragdoll

hello, I have a problem and I can’t find a solution, I have an oxygen Gui bar when you are in the water you lose oxygen, however in my game I have the possibility of Ragdoll with F, and when I do it in water, the oxygen bar regenerates it even though it shouldn’t.

Here is the local script in the Gui:

local bar = script.Parent.BarBG.Bar
local oxygenLabel = script.Parent.BarBG.OxygenAmount

local maxOxygen = 100
local currentOxygen = maxOxygen

local timePerLoss = 0.3

local isSwimming = false

local char = game.Players.LocalPlayer.Character
local hum = char:WaitForChild("Humanoid")


hum.StateChanged:Connect(function(oldState, newState)
   
   
   if newState == Enum.HumanoidStateType.Swimming then

   	isSwimming = true
   	
   else
   	isSwimming = false
   end
end)


while wait(timePerLoss) do
   
   
   if isSwimming then
   	
   	currentOxygen = math.clamp(currentOxygen - 1, 0, maxOxygen)
   	
   else
   	currentOxygen = math.clamp(currentOxygen + 1, 0, maxOxygen)
   end
   
   
   if currentOxygen < 1 then
   	
   	hum.Health = 0
   end
   
   
   local barScale = currentOxygen / maxOxygen
   bar:TweenSize(UDim2.new(barScale, 0, 1, 0), "InOut", "Linear", timePerLoss)
   
   oxygenLabel.Text = currentOxygen .. " / " .. maxOxygen
end

Ragdoll ≠ swimming. Make it ignore state changes if you’re ragdolling.

1 Like

how and where should I add it to the script? I am a beginner…

Make it end the function for StateChanged instantly if you’re in the ragdoll’s state (or the ragdoll value is on)

1 Like

ok, but I don’t know how to write it :confused:

Create or find a way to detect ragdoll status as a boolean. Then, stuff this at the top of StateChanged:
if yourcondition then return end

You don’t need to find the state as a boolean, there’s a humanoid state called Ragdoll
if newstate == Enum.HumanoidStateType.Ragdoll then return end

1 Like

thank you so much it works ! :smiling_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.