Need Help With Oxygen Script

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…

1 Like

I think I know where he got his whole script.
A LITERAL TUTORIAL VIDEO 4 YEARS AGO

1 Like

Um, before I even try to help, it would be beneficial to me and others reading this if the code wasn’t all broken up. But it seems as though this code is not original, no wonder you guys don’t understand it.

But yeah, please format it so it doesn’t look like a turkey sandwich.

go to the link I put up it has everything you need to know.

1 Like

The problem is your not checking if the head is underwater or not over and over again.

1 Like

First of all it wasn’t the script I wrote, he was the one that imported it. And second of all, I copy and pasted the script in, and in the preview it wasn’t broken up, but when I published it, it was broken up.

I also did “format” it.
I stated what is wrong, and what needs to be fixed.

So what your saying is you copied a script from your friend who copied a outdated YouTube tutorial and because it didn’t work you came here to try and fix it even though there are underwater scripts that you can easily implement a drowning feature into?

1 Like

Why not use this and have your friend add some code that hurts you after some time?
A Underwater Script
Oh wait it already has one.

1 Like

The Scripting Support category is intended for receiving help on things you created. If it’s not of your own creation, there is very little to go from because you don’t even understand the concept behind how the script works, so you probably won’t understand the fixes either.

You need to apply basic debugging to try and isolate the issue, because posting your entire system and asking for fixes rather than directions is inappropriate use of this category. We don’t even have a given place to look because there’s just a mound of pasted code and no apparent effort from your end to attempt to resolve the issue you’re facing.

There are various resources in and around the toolbox that you can reference for help fixing this kind of a system as well, or modify such that it can include the features you’re looking for. Keep in mind that this is not a do-my-work category.

Refer to our category guidelines for more information on how to properly use this category.

As for the formatting debacle, no you haven’t formatted it because there are still parts of the script outside of a code block. When posting code to the DevForum, please include your code within a code block so it’s easier to read. You can do so with three backticks above and below your code.

```lua
– Code
```

4 Likes

I did attempt to fix it, but like I said, I know nothing about scripting or troubleshooting in scripts etc.

My friend was offline, I asked the forum (not knowing this was somebody elses script) and didn’t get the answer I was looking for.

I attempted, and I failed. I don’t know how to script much at all, and I didn’t know that there would be a different way of pasting code.
I have watched the video, read the about Scripting Support Catagory, and I will not make this mistake again.

Selling and trading direct or proxy access to the Developer Forum is forbidden. You may not make posts on behalf of other users unless explicitly allowed by that category.
Official Rules of the Roblox Developer Forum

Your friend should be posting this, not you.

Show that attempt

Use this to learn scripting

Thanks for the link.
It was quite useful and I appreciate the “show that attempt” bit.