I need help with a drowning script

  1. I want to achieve it after the player has been in the water for 30 seconds then it starts to drown.

  2. The drowning happens immediately.

  3. I have tried to look for solutions all over the dev forum. I found this script because it was marked as a solution but I cannot time it to drown after 30 seconds. I have tried but here is the original script.

local Character = script.Parent
local Humanoid = Character.Humanoid
local Swimming = false

Humanoid.StateChanged:Connect(function (oldState, newState)
	if newState == Enum.HumanoidStateType.Swimming then
		print("Swim")
		Swimming = true
	elseif oldState == Enum.HumanoidStateType.Swimming and newState ~= Enum.HumanoidStateType.Jumping then
		print("No swim")
		Swimming = false
	elseif oldState ~= Enum.HumanoidStateType.Swimming and oldState ~= newState then
		print("No swim")
		Swimming = false
	end
	
	while Swimming do
		Humanoid:TakeDamage(5)
		if Swimming then
			wait(1)
		end
	end
end)
5 Likes

U should put wait outside of if statement

Do some math! Since a humanoid normally has 100 health, and you want it to decay over 30 seconds, you can use this method to get the ideal value:

Humanoid:TakeDamage( 100 / 30 ) --Max health / Time

As long as you run this once a second, you should be set. You got it!

But it would delay the “No swim” part of the script too.

Where do I insert this line of code?

Replace it with the line that also contains Humanoid:TakeDamage(). That is where damage is applied to the character.

1 Like

Thanks! I will try it. 30 characters

By the way, where do I put the five damage part of the script?

while Swimming do
		Humanoid:TakeDamage(5)
		wait(1)
	end

Do this instead of ur version, why would u check if a player is swimming if u already know that while loop will only run when player is swimming

1 Like

You should be replacing that with the line I suggested. It’s essentially the same as the previous line, however this new one has different numbers as the argument.

Thanks! 30 characters with more characters

The wait for 30 seconds is not there. What do I edit to put that in?

It did not work. 30 characters

So u make it so every one second the humanoid is taken 5 damage, that means it will last 20sec for player to die

Oh, you want to drown the player immediately after 30 seconds? Then something like this should do the trick:

   for i = 1, 30 do
        if not Swimming then return end
   end
    
   Humanoid:TakeDamage(100)

This script will check every second for 30 seconds if the player is swimming. If it ever returns false, the code will stop. If it is able to finish counting to 30 without interuptions, the player will be killed.

Edit: Clarification, you’ll replace the while loop with this.

1 Like

No, I would just want it to take 5 damage every 1 second after 30 seconds of being in the water.

Ah, that explains it. Then you’d combine the two loops to create something like this:

for i = 1, 30 do
      if not Swimming then return end
      wait(1)
end

while Swimming do
	Humanoid:TakeDamage(5)
	wait(1)
end

Try making a touch part or using region3 instead, it will be much easier.

Maybe u should put the for loop into while loop

No, that would be a terrible idea. If you did that, it’d wait 30 seconds before every single chip of damage. Essentially, it would take minutes for the player to die.