Make ChangeState('Swimming') override automatic Humanoid state changes

A friend brought this thread to my attention, trying to do the same thing
(also, LOL at me being rude years ago, sorry haha)

The solution was simple, disable the other states while trying to do this

(Looks like Humanoid:SetStateEnabled was released in July 2015, a month after this thread? lol)
image

Example code in a localscript:


wait(.2)

local char = game.Players.LocalPlayer.Character
local root = char:WaitForChild'HumanoidRootPart'
local hum = char.Humanoid
local runservice = game:GetService'RunService'
local uis = game:GetService'UserInputService'
local waterline = workspace:WaitForChild('waterline')
local bodyforce_name = 'water BodyForce'




local tweenservice = game:GetService('TweenService')

local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
local velocity_fade = tweenservice:Create(root,tweenInfo,{Velocity = Vector3.new()})

local time_entered_water = tick()
local time_left_water = tick()
local underwater = false

runservice.Stepped:Connect(function()
	
	if root.Position.y < waterline.Position.y then
		if not underwater then --when you first enter the water
			underwater = true
			time_entered_water = tick()
			
			root.Velocity = root.Velocity * .1
			
		end
		
		if tick() - time_left_water > .5 then --prevents weird constant state changing when near surface
			hum:ChangeState("Swimming")
		end
		
		workspace.Gravity = 0

		
		hum:SetStateEnabled('Running',false)
		hum:SetStateEnabled('RunningNoPhysics',false)
		hum:SetStateEnabled('Climbing',false)
		hum:SetStateEnabled('GettingUp',false)
		hum:SetStateEnabled('Jumping',false)
		
		local moving = hum.MoveDirection ~= Vector3.new()
		local trying_to_rise = uis:IsKeyDown(Enum.KeyCode.Space) --consider mobile jump button gui & controller's ButtonA
		
		if not moving and not trying_to_rise and tick() - time_entered_water > .2 then
			velocity_fade:Play()
		else
			velocity_fade:Cancel()
		end

	else
		
		if underwater then
			underwater = false
			
			time_left_water = tick()
			
			workspace.Gravity = 196.2
	
			
			hum:SetStateEnabled('Running',true)
			hum:SetStateEnabled('RunningNoPhysics',true)
			hum:SetStateEnabled('Climbing',true)
			hum:SetStateEnabled('GettingUp',true)
			hum:SetStateEnabled('Jumping',true)
		end
		
	end
end)



10 Likes