#Oxygen bar
19 messages · Page 1 of 1 (latest)
if you know the height of the water, you can track if the player's head is close enough
^
local bar = script.Parent.BarBG.Bar
local oxygenLabel = script.Parent.BarBG.OxygenAmount
local maxOxygen = 100
local currentOxygen = maxOxygen
local timePerLoss = 0.3
local timeToWait = 1
local isSwimming = false
local char = game.Players.LocalPlayer.Character
local hum = char:WaitForChild("Humanoid")
local head = char:WaitForChild("Head")
while wait(timeToWait) do
if head.Position.Y < 255 then
isSwimming = true
print("swimming set to true")
else
isSwimming = false
print("swimming set to false")
end
end
while wait(timePerLoss) do
print("second while started")
if isSwimming == true 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
print("level set")
end
if you want to make it even more realistic you can offset the place of the head your tracking
for some reason, the second while statement doesnt run at all
i.e closer to the nose for exmaple
cause the first while statement is running 😅
oop im a noob
remember scripts are one lane 🙂
so essentially one while per script?
they execute logic in order
nope you can do stuff to have multiple loops per script
like using task.spawn to create a new pseudo thread
task.spawn(function()
while task.wait() do
end
end)
print("this will print")
while task.wait() do
end
print("this will NOT print")
thats pretty useful thanks for the help
no worries
it works now