If a player is touching the water part, a blue bar GUI will appear on their screen, showing their air. If they are in for too long, it will start to deal damage until they get out of the water. However, it doesn't show the air bar decreasing, and it also doesn't stop dealing damage if they get out.
local air = script.Parent local hitbox = game.Workspace.WaterHitbox local airBarSize = air.CurrentAir.Size.X.Offset air.Enabled = false hitbox.Touched:connect(function(part) local humanoid = part.Parent:FindFirstChild("Humanoid") if (humanoid ~= nil) then --if a humanoid exists air.Enabled = true --show air bar repeat wait (0.01) airBarSize = airBarSize - 2 until airBarSize == 0 repeat humanoid.Health = humanoid.Health - 1 wait() until not airBarSize == 0 end end) hitbox.TouchEnded:connect(function(part) local humanoid = part.Parent:FindFirstChild("Humanoid") if (humanoid ~= nil) then airBarSize = 544 air.Enabled = false end end)
personally i'd add a boolean value in the player somewhere named like "Drowning" or something then inbetween lines 9 and 10 do
humanoid.Parent.Drowning.Value = true
then to fix the endless damage dealing on line 17 do
until not airBarSize == 0 or humanoid.Parent.Drowning.Value == false
then inbetween lines 23 and 24 do
humanoid.Parent.Drowning.Value = false
Now for the airbar not changing or whatever I think it's because you're using a script and like server scripts can't access playergui, idk tho. I might be wrong about that part but i'd still use remotes to deal the damage while the bar shrinking is all client in a localscript.
This is due to line 3. You’re setting airBarSize
to the value of X.Offset
at the time you assigned it the variable.
For example, if the current value of the Offset
was -25, local airBarSize = air.CurrentAir.Size.X.Offset
would be the same as local airBarSize = -25
. Another thing to add on is that your script will error if you try to assign X
, Y
, XOffset
, or YOffset
from a script. These cannot be written from scripts, but manually in the Explorer. You’ll have to use UDim2.new()
to set the size.
What I would do for line 3 is simply set it to the object:
local airBar = air.CurrentAir
But you can out this question I asked to get a better understanding of this common mistake:
https://scriptinghelpers.org/questions/61116/when-do-you-store-a-property-as-a-variable