How would I script an oxygen bar by reading terrain voxels?


Here’s the output I don’t see anything regarding the oxygen script

It says there was an error on line 23 of a server script, that is also unrelated right?

I realized I had an error in the server script, can you recopy and paste that code and see if it works now?

This is the result that I got:
https://gyazo.com/9e29473c61b17c016e78e6d3b60dff5b

Maybe just replace the local script code with this?

local player = game.Players.LocalPlayer

local background = script.Parent
local Bar = background:WaitForChild("Bar")
local OxygenText = background:WaitForChild("OxygenText")

OxygenText.Size = UDim2.new(1,0,1,0)

local oxyValue = player:WaitForChild("OxygenValue")

while wait(0.3) do
	Bar:TweenSize(UDim2.new(player.OxygenValue.Value/1000,0,1,0), "Out", "Quad", 0.3)
	OxygenText.Text = "Oxygen "..player.OxygenValue.Value.."/1000"
end
1 Like


I get this error message for the server script

Try replacing the server script with this:

game.Players.PlayerAdded:Connect(function(player)
	local oxygen = Instance.new("IntValue", player)
    oxygen.Value = 1000
    oxygen.Name = "OxygenValue"
	local increaseRate = 3 --how fast it increases
	local decreseRate = 2 --how fast it decreases
    local maxOxygen = 1000 --change accordingly

	while wait() do
		local char = player.Character or player.CharacterAdded:Wait()
		local castRay = Ray.new(char.Head.Position, Vector3.new(0,-10,0))
		local whiteList = {char}
		local hit, hitposition = workspace:FindPartOnRayWithIgnoreList(castRay, whiteList)
		local underwater = false
		if not hit then
			local castRay2 = Ray.new(char.Head.Position + Vector3.new(0,1000,0), Vector3.new(0,-1000,0))
			local hit2, hit2position = workspace:FindPartOnRayWithIgnoreList(castRay2, whiteList)
			if hit2 then
				underwater = true
			end
		end
		if underwater == false then
			oxygen.Value = math.clamp(oxygen.Value + increaseRate, 0, maxOxygen)
		else
			oxygen.Value = math.clamp(oxygen.Value - decreseRate, 0, maxOxygen)
		end
		print(oxygen)
	end
end)
1 Like


this happens but the oxygen bar doesnt do anything
also the oxygen level doesnt go down either

If you change the print to,

print(oxygen.Value)

Does the value change?


No it just prints the value of the oxygen

Weird, it works for me. So this is in ServerScriptService?

1 Like

yea its in server script service

if its in a game try sending the link to the game and I can get the model from there

This is the last thing I can think of that you could try for the server script. I am really confused though because it works for me. Are you using terrain water?


game.Players.PlayerAdded:Connect(function(player)
	local increaseRate = 3
	local decreseRate = 2
	local oxygen = Instance.new("IntValue", player)
	oxygen.Value = 1000
	oxygen.Name = "OxygenValue"
	wait(3)
		while wait() do
		local char = player.Character or player.CharacterAdded:Wait()
		local castRay = Ray.new(char.Head.Position, Vector3.new(0,-10,0))
		local whiteList = {char}
		local hit, hitposition = workspace:FindPartOnRayWithIgnoreList(castRay, whiteList)
		local underwater = false
		if not hit or not hit:IsA("Terrain") then
			local castRay2 = Ray.new(char.Head.Position + Vector3.new(0,1000,0), Vector3.new(0,-1000,0))
			local hit2, hit2position = workspace:FindPartOnRayWithIgnoreList(castRay2, whiteList)
			if hit2 and hit2:IsA("Terrain") then
				underwater = true
			end
		end
		if underwater == false then
			oxygen.Value = math.clamp(oxygen.Value + increaseRate, 0, 1000)
		else
			oxygen.Value = math.clamp(oxygen.Value - decreseRate, 0, 1000)
		end
	end
end)
1 Like

Yea im using terrain water also ill try your script

It worked! And it also works in the way I wanted it to. Thanks so much!

1 Like

I’m so glad it works :smile: ! Let me know if you have any questions.