How would you make a drowning system?

My idea was to originally name a swimming stamina system like genshin or zelda, but i’m failing miserably at it. I’m not sure where I went wrong with this though
the main issue i’m having is with the exiting of the water. stamina does not regenerate.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService") 
local running = false
local swimming = false
local p = script.Parent.Parent.Percentage
local RS = game.ReplicatedStorage
local PlayerStatus = RS.PlayerStatus
local Humanoid = Character:WaitForChild("Humanoid")
local stamina = PlayerStatus.Stamina.Value
local swmval = PlayerStatus.Swimming.Value
local runval = PlayerStatus.Running.Value
local root = Character:WaitForChild("HumanoidRootPart")
stamina = math.clamp(stamina,0,100)

Humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Swimming and running == false then
		print("Swimming")
		swimming = true
		Character.Humanoid.WalkSpeed = 32
		while stamina > 0 do
			stamina = stamina - 0.3
			script.Parent:TweenSize(UDim2.new(stamina / 100,0,1,0), "Out", "Linear", 0)
			wait()
			if stamina == 0 then
				Character.Humanoid.Health = 0
			end
		end
		if oldState == Enum.HumanoidStateType.Swimming and newState == Enum.HumanoidStateType.Running then
		swimming = false
		Character.Humanoid.WalkSpeed = 16
		while stamina < 100 do
			stamina = stamina + 1
			script.Parent:TweenSize(UDim2.new(stamina / 100,0,1,0),"Out", "Linear", 0)
			end
		end
	end
end)

any help would be appreciated, thank you!

You could set the walk speed to 0. And then casually increase downforce to drag the player under.
Check if the player’s head is surrounded by water. If they are surrounded by water deplete their oxygen.
Once the oxygen has been depleted you can start depleting their health.

1 Like

I don’t think its necessary to use tweenservice to change the amount of stamina left. Just decrease it by a small amount, wait a small amount and then repeat until stamina is finished

let me rephrase, the main issue i’m having is with the exiting of the water. stamina does not regenerate.

try checking if the newstate is anything other than swimming, people are usually in the falling state i think

1 Like

As long as you handled the head check like I mentioned, then regenerating stamina or oxygen or any other stat should be easy as the head check would return air instead of water.

Which property of the head would I use to check that?

There’s no property, you would need to check the voxels around the player’s head and return the material that’s present.

what kind of object is percentage?

forget about that, that’s for other stuff

im trying to run the script in studio so its easier to debug

How, If i may ask?
-------char limit--------

just copy script and put it in baseplate i looked at it quickly and apart from percentage value it doesnt seem to need anything else

What i did in my case is:
creating a stamina system like the humanoid Health system. In my game, the stamina is usefull for attacks and moves so i needed it. Then all i had to do was reducing the stamina amount every seconds. if the stamina reach a certain amount near 0, just delete the body mover inside the player and boom, here you go, drowning system

also the stamina system i did got the same regeneration script as the health roblox gives by default

I use Terrain:ReadVoxels to read a 4x4x4 area around the player’s head or any position for that point. Then I get the first material at array Material[1][1][1]. If it’s equal to water then the player’s head is under water. Otherwise if it’s equal to air the player is not under water.

This is pretty performance heavy, so you don’t have to run it unless the player is in Swimming State. If you’re not using smooth terrain then it would be up to you to check the surroundings for what you’re using as water.

1 Like

Thanks very much
--------chars-------

1 Like

or You can just check the water height compared to your HRP height, can’t you?

Nah, if parts of your level goes under your typical “sea” level then it wouldn’t work. Your new limitation would be you can’t build areas below sea level without disabling the check altogether. Possibly if you know the player is in an area below sea level that isn’t filled with water you could disable the check altogether.

However, my solution handles that with the fact that if you’re not in water the volume will return Enum.Material.Air otherwise it doesn’t even run if you’re not swimming.

oh, well percentage is a textlabel. the issue has been resolved, however. thank you for your time!

1 Like