Hello, Dev’s!
I’m making a game that takes place on the moon, and I’m doing a good amount of progress on it.
recently, a friend of mine made me a GUI script, which is an oxygen bar GUI that drops whenever the player touches the “sand” terrain (the material that I use to build the terrain in my game), this was meant to be a way for the player to lose oxygen whenever it goes outside the moon surface, but I realize that it’s a bad idea since players can jump over the sand before they lose oxygen (aka cheat the system).
I thought of making the script function only when a player is touching a part called “NoOxygen” for example, but since I’m more of a builder than a scripter, I couldn’t find a way to make it work when touching a part, my scripting skills are limited and my friend can not help me since he’s busy making his game and etc.
local Player = game.Players.LocalPlayer
local BreathUI = script:WaitForChild("OxygenGui")
local Gui = nil
local Running = false
local Percent = 100
local InWater = false
local Func = {}
local GuiTab = {}
-- Functions --
Func.checkFor = function(parent, ...)
for i, objName in pairs({...}) do
if not parent:findFirstChild(objName) then
return false
end
end
return true
end
Func.Gather = function(Objects, recurse, tabby, Type)
for i, Object in pairs(Objects:GetChildren()) do
if Object:IsA(Type) then
table.insert(tabby, Object)
end
if recurse then
Func.Gather(Object, true, tabby, Type)
end
end
return tabby
end
Func.CheckTouch = function(Pos, Enam)
local cellLocation = Workspace.Terrain:WorldToCellPreferSolid(Pos)
local cellMaterial = Workspace.Terrain:GetCell(cellLocation.X, cellLocation.Y, cellLocation.Z)
if cellMaterial == Enam then
return true
end
return false
end
Func.HandleGui = function(bool)
if bool then
local Gui = BreathUI:Clone()
Gui.Parent = Player.PlayerGui
table.insert(GuiTab, Gui)
return Gui
else
for i, g in pairs(GuiTab) do
g:Destroy()
end
GuiTab = {}
end
end
Func.HandleUIAnimation = function(gui, up, bool)
if not Running then
local x, y, z, t
if up then
x, y, z, t = Percent, 100, 1, 0.001 -- How many seconds between each percentage of air is gained.
else
x, y, z = Percent, 0, -1, 0.1 -- How many seconds between each percentage of air is lost.
end
local c = coroutine.create(function()
Running = true
for i = x, y, z do
if bool ~= InWater then
break
end
wait(t)
local num = i * 0.01
gui.OxFrame.OxBar.Size = UDim2.new(num, 0, 1, 0)
gui.OxFrame.OxLabel.Text = "Oxygen "..i.."% "
Percent = i
end
Running = false
end)
coroutine.resume(c)
end
end
Func.HandleHealth = coroutine.wrap(function()
while true do
if InWater and Percent <= 0 then
wait(0.5) --How many seconds between each time health is taken away for no oxygen.
Player.Character.Humanoid:TakeDamage(5) -- How much health is taken away for no oxygen.
else
wait()
end
end
end)
-- Run --
repeat wait() until Func.checkFor(Player.Character, "LeftFoot", "LeftHand", "LeftLowerArm", "Head", "LeftLowerLeg", "LeftUpperArm", "Humanoid", "LeftUpperLeg", "LowerTorso", "RightFoot", "RightHand", "RightLowerArm", "RightLowerLeg", "RightUpperArm", "RightUpperLeg", "UpperTorso")
Gui = Func.HandleGui(true)
Func.HandleHealth()
while wait() do
if Func.CheckTouch(Player.Character.RightFoot.Position + Vector3.new(0, 2.5, 0), Enum.CellMaterial.Sand) then
InWater = true
Func.HandleUIAnimation(Gui, false, true)
else
InWater = false
if Percent < 100 then
Func.HandleUIAnimation(Gui, true, false)
end
end
end
I hope to see your solution, Thank You