Preventing Player From Jumping While Swimming?

I’d like to prevent the player from jumping out of the water while at the water’s surface. However, even if JumpPower is 0, or if Jumping is no longer an enabled StateType, you can still hop out of the water. My only other idea was to unbind spacebar, but then rebinding jump becomes a hassle.

Note that unbinding spacebar does work, but I’m assuming that hopping out of the water is actually a part of swimming and thus disabling jump does nothing because a jump is built into the swimming state.

Any pointers?

4 Likes

Well there would be a few things you can do here.

  1. You can make a vector where anytime the jump state in the character is set to true it turns on pushing down to counter the force of the jump.
  2. Do the same thing as above but with gravity instead.
  3. Put invisible walls around the water/above the water so they cant jump.
1 Like

thank you, but none of these are what i’m looking for. i want to prevent the player from entering the “Jump” state at all while in the water, as i do checks for humanoidstatetype that determine certain parts of my GUI.

1 Like

Have you tried listening to StateChanged event (on Humanoid) that will allow you to know if it changed to swimming so you can block jumping but when not swimming, you can unblock jumping?

1 Like

I saw this post and was curious enough to try it myself, and you can definitely accomplish it the way ArtFoundation mentions.

I’ve attached a snippet of what this may look like below, if you want to better understand how it works, the documentation for SetStateEnabled is pretty helpful!

--Disable the player's ability to jump when they start swimming
if (newState == Enum.HumanoidStateType.Swimming) then
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
		
--Re-enable the player's ability to jump when they stop swimming	
else 
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
end


--Connect an event callback to when the player's humanoid changes state
humanoid.StateChanged:Connect(<functionName>)
3 Likes

Try this:

  1. Add a Part and name it: “NoJumpPart” and move it into the part where you don’t want the player to jump at. Make sure to change its Transparency to 1 and CanCollide off. And make sure the size is the same size as the water or the area where you don’t want the player to jump.
    Note: Don’t put it above, just put it inside the water.

  2. Add a script inside the part and I will type out a sample code

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid.JumpPower = 0
    end
end)

Not the exact code but similar.

P.S. I could be wrong, but there might a few errors I made.

2 Likes

This code when placed inside a LocalScript in StarterPlayer in StarterCharacterScripts

local humanoid = script.Parent:WaitForChild("Humanoid")

humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Swimming then
		humanoid.JumpPower = 0
	else
		humanoid.JumpPower = 50
	end	
end)

this is what i use already. it does work to an extent, but there’s still a point where the player can hold space and will leave the water and enter the Freefall state for a short period of time. that’s what i want to avoid (and i can’t exactly disable the Freefall state).

1 Like

i have already tried this and the player can still leave the swimming state while holding space at the surface of the water.

yes, i’ve been getting plenty of returns of Freefall when holding space at the surface of the water. this is something i want to avoid.

Ah yeah I see, in that case you could try adding a condition for re-enabling JumpState that the newState can’t be FreeFall, or simply a debounce.

Not a perfect solution , but it may help make it a bit more accurate, depending on how your water is set up

Edit: Never mind, glad this helped!

1 Like

ah, i don’t know why i didn’t think to add the .02 second wait debounce. thanks for the reminder… whew, simpler than i thought it would be.

edit to reply to your edit: i know you think it wouldn’t be helpful, but it actually does! the wait that allows us to continue checking if we’re still in the water, even after the humanoid says we’ve left it, allows us to confirm whether or not it was just a flash of humanoid jank. with the debounce in line, everything still knows we’re in the water no matter what the humanoid says.

1 Like

Oh ok perfect! And hey no worries, sometimes the simplest solutions elude us lol

1 Like