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.
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.
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?
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>)
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.
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.
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).
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.