#Oxygen bar

19 messages · Page 1 of 1 (latest)

lusty garnet
#

I'm having trouble figuring out how to tell when a players head is above the water. Currently the way my script works is just by detecting if the player is swimming. The player can't swim to the surface for air though with this system. Any suggestions on what I could try?

eternal plinth
#

if you know the height of the water, you can track if the player's head is close enough

lusty garnet
#
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
rustic panther
lusty garnet
#

for some reason, the second while statement doesnt run at all

rustic panther
#

i.e closer to the nose for exmaple

rustic panther
lusty garnet
#

oop im a noob

rustic panther
#

remember scripts are one lane 🙂

lusty garnet
#

so essentially one while per script?

rustic panther
#

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")
lusty garnet
#

thats pretty useful thanks for the help

rustic panther
#

no worries

lusty garnet
#

it works now