I’m trying to make the terrain water in my game take away player health. The problem is that when the player gets into the water it takes away 1/100 health and stops. It doesn’t continue taking away player health.
Here is my hierarchy:
The swimming state is only triggered when the player is moving, in water, and changing speeds.
If they continue to swim at the same speed, or stop moving, the swimming state is no longer true.
You will need to check if they are in water another way, also you won’t be able to deal damage from a local script if the game is FE enabled. Are you in experimental mode?
Ah, I didn’t know that.
I’ve only tested that script out in Playtesting in Studio. Also, it isn’t a local script I was using. So, if I want to keep triggering the damage while still swimming at a constant speed, how would I go about doing that with the script I have?
Oh, I don’t know why I thought I saw a local script. Maybe, because it is located inside the StarterCharacter? My apologies.
Your best bet is to either;
A. Set up a function with Humanoid.StateChanged, once swimming is the state, begin dealing damage. Have it continue to deal damage every second, until the state is changed to one of the other basic states that would be triggered once you leave the water (running, platformstanding, jumping, gettingup).
B. On the client, send a remote event when the Swimming animation is played to begin the damage, and send another remote event once the swimming animation is not playing to stop the damage. (This is likely to be abused by exploiters)
The property right now is only used for the sake of informing developers that their game is potentially outdated. It also manages a notice on the website under the game’s play button which says the game may be out of date. Flipping it otherwise has no effect.
You only need to call take damage once. Your actually taking 2 damage every second (wait(1))
Your while loop runs every second with wait 1,
So every second your checking if the player is in the water.
Inside that first if statement, you have take damage.
Then in that same second, your still checking if you’re in the water (which you would be) and it takes an extra health.
If the desired effect is removing 2 health, then adjust take damage(1) to take damage(2) and remove the second if statement.
Im on my phone.
While wait(1) do
If statement then
Take damage(2)
End
End
The code above that I marked as the solution works. The longer I stay in the water, the more life I lose. That’s what I originally intended for it to do. It’s a rough script , but it works, and that’s all I was wanting
For the second if statement, you need to do an explicit check of if the Humanoid’s StateType is still swimming. If you don’t, then the condition automatically passes as true because Enum.HumanoidStateType.Swimming is not a falsy value.
local Humanoid = script.Parent.Humanoid
while true do
if Humanoid:GetState() == Enum.HumanoidStateType.Swimming then
Humanoid:TakeDamage(1)
end
wait(1)
end
I’m not too sure what the second check was for so I removed it out of that code sample up there.
The problem is that this script doesn’t keep doing damage to the player.
I want the script to keep repeating itself until the player is dead or stops swimming. That’s why the second check was put in the “solution” code. Because it repeated the damage action and kept taking away player health.
I tried to tweak it to this:
Well, that would crash your script. Since you are looping infinitely with no wait in your repeat loop.
Adding the second check also doesn’t make it loop the damage, I’m really curious how the damage is looking right now when you swim. Can you post a gif or a link to the game?
You don’t? Hmm, what platform do you play on? I’ll try to enable all.
I still need assistance, just to at least get an answer to why the script isn’t or is working.
Edit: I enabled everyone to play and all platforms now
Note: the script for the deadly water right now is this one: