The most challenging part of this system is going to be detecting whether or not a player is in a safe zone. I think you could go about this 2 ways:
One is you have invisible parts in safe zones that when players are touching them, they stop the depletion of oxygen and/or start restoring it.
The other is that you do the opposite - have invisible parts in the dangerous areas that when touched deplete the player’s oxygen levels.
local touchingPlrs = {}
local PLRS = game:GetService("Players")
local bricks = -- your bricks/zones for depleting oxygen
for _, brick in pairs(bricks) do
brick.Touched:Connect(function(hit)
local plr = PLRS:GetPlayerFromCharacter(hit.Parent)
if (plr) then
touchingPlrs[plr] = true
while touchingPlrs[plr] do
wait()
-- deplete oxygen
end
end
end)
brick.TouchEnded:Connect(function(hit)
touchingPlrs[plr] = false
end)
end
You can create a NumberValue or an IntValue inside the player to store the oxygen and try something like this:
local function isInsideSafezone(character)
-- I would use region3 here
return true/false
end
local function removeOxygen(player, quant: number)
local character = player.Character or player.CharacterAdded:Wait()
local isProtected = isInsideSafezone(character)
if not isProtected then
local oxygen = player.Stats.Oxygen
oxygen.Value -= quant
if oxygen.Value <= 0 then
print(player.Name .. ' died.')
end
end
end
Yep the detecting safe zone problems have been solved before through modules in community resources such as ZonePlus if you are looking for a region3 like method. The API also is explained really neat so I guess try checking it out.