So me and my friend are working on this RPG game ( we are looking for a scripter here is the link to apply https://devforum.roblox.com/t/open-looking-for-a-scripter-invasion-rpg/443576 )
Here is an oxygen script my friend made. We know what the issue is but we don’t know how to code it probably.
local SurfaceGui = script.Parent
local OA = SurfaceGui.Frame.OxygenBar
local Plr = game.Players.LocalPlayer
local overlay = script.Parent:WaitForChild(“Overlay”)
overlay.Visible = false
repeat wait() until Plr.Character
local Hum = Plr.Character:WaitForChild(“Humanoid”)
local cOxygen, MaxOxygen= 20, 20 – 1 = 1 second of air
local Swim = false
local UIS = game:GetService(“UserInputService”)
local T = game.Workspace:WaitForChild(“Terrain”)
local Ended = true
function UnderWaterCheck(Head)
if Head then
local InWater = true
local Offset = Head.Size / 2
local P1, P2 = (Head.Position - Vector3.new(1, 0, 0)) - Offset, (Head.Position + Vector3.new(1, 0, 0)) + Offset
local R3 = Region3.new(P1, P2):ExpandToGrid(4)
local Tbl = T:ReadVoxels(R3, 4)
for _, Material in pairs(Tbl[2][1]) do
if Material == Enum.Material.Air then
InWater = false
end
end
return InWater
end
end
Hum.Swimming:connect(function()
if not Swim then
SurfaceGui.Frame.Visible = true
Swim = true
end
UIS.InputBegan:connect(function(Pressed)
if Swim and Pressed.KeyCode == Enum.KeyCode.Space then
Ended = false
UIS.InputEnded:connect(function(Released)
if Swim and Released.KeyCode == Enum.KeyCode.Space then
Ended = true
end
end)
repeat wait()
if not UnderWaterCheck(Hum.Parent.Head) and Swim then
Swim = false
SurfaceGui.Frame.Visible = false
cOxygen = MaxOxygen
OA:TweenSize(UDim2.new(cOxygen/MaxOxygen,0, 1,0), "Out", "Linear", .3, true)
Ended = true
end
until Hum.Health == 0 or Ended or not Swim
end
end)
Hum.Running:connect(function()
if Swim then
SurfaceGui.Frame.Visible = false
Swim = false
cOxygen = MaxOxygen
end
end)
end)
while wait(1) do
OA:TweenSize(UDim2.new(cOxygen/MaxOxygen,0, 1,0), “Out”, “Linear”, 1, true)
if Swim then
if cOxygen - 1 > 0 then
cOxygen = cOxygen - 1
else
repeat wait(0.1)
Hum.Health = Hum.Health - 1
until Hum.Health == 0 and Swim
end
else
if cOxygen ~= MaxOxygen and Hum.Health > 0 then
cOxygen = MaxOxygen
end
end
end
The reason why it won’t work is because it loops the “drowning part where you lose your HP” so even when you leave the water, you keep drowning. I don’t know how to code so i need your help lol…