Terrain water script that takes away player health

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:
51%20AM
And my script:


I don’t know how to go about making the water keep taking away player health

3 Likes

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)

1 Like

The ability to disable FilteringEnabled has been retired for some time now. FilteringEnabled is standard for every DataModel.

Oh, I thought due to the property for FE enabled still showing under workspace, it was still toggle-able.

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.

2 Likes

Perhaps try using either Humanoid.Floor.Material or use a ray cast like this

local Ray = Ray.new(RootPart.Position, Vector3.new(0, -3, 0))
local FloorPart = workspace:FindPartOnRay(Ray, IgnoreDescendantsObject)
1 Like

Thanks @RamJoT and @Hazania but I found a solution. Here’s what I ended up with:


The hierarchy is still the same (starterPlayer then starterCharacterScripts)

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

1 Like

Have you tested your code? It does not do what you originally asked for or intended.

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

I don’t think it is actually working as you intended, but more or less appearing to. I can explain your code in detail if you would like.

1 Like

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.

1 Like

The problem is that this script doesn’t keep doing damage to the player.
30%20AM
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:


But it didn’t work

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?

1 Like

Here’s the link to my game:
https://www.roblox.com/games/4079522726/CandyLand?refPageId=1a57dfa3-13ad-434f-8144-3f3038ee3836
Yeah, I’m not sure what made that script work :woman_shrugging: but it did.

Looks like I don’t have permissions to play.

If you believe you no longer require assistance, I will leave you to it! Let me know if you end up needing any more help with this.

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:

Oh, I was attempting on my mobile device but I can get on my PC.

I can definitely still help you. I thought you believed that there was no longer a problem, so I was going to leave it at that.

Edit: @LandofLee1620 I can’t find the water, this map is massive!

1 Like