Help with brainstorming a script

I’m currently making a oxygen system for me game, at the moment i have the oxygen bar done.

The oxygen system works with a value, if the value reaches 0 the player is supposed to die.

Now what i’m going for is making the value go down if you are not in a safe zone, but i’m still brainstorming on how i’d do that.

If someone could give me a tip or idea of a simple way to do that i would really appreciate.

Thanks :slightly_smiling_face:

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.

Hope this helps!

1 Like

Yeah that was my best idea so far. And yes,

1 Like

relatively simple.

use Region3 to detect safezone areas. do a check if somebody is inside the region before depleting their oxygen levels.

1 Like

There are 2 ways I can think of how I would do this:

(for either of these to work I’m assuming you have blocks/zones set up where you want the oxygen to deplete)

  1. Use Region3
  2. Use the .Touched and .TouchEnded

Example of option number 2:

 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

Here’s a tutorial on how to use Region3:

2 Likes

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
1 Like

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.

1 Like