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

while true do
game:service(“RunService”).RenderStepped:wait()
game.Players.LocalPlayer.Character.Humanoid:ChangeState(‘Swimming’)
end

still produces this buggy result because it has to fight for it.

1 Like

How about no. I just want to be able to force swimming and not have it forced back every other frame. I don’t want to toggle auto state changing because of freefalling, physics, jumping, running, landing and such stuff that will bug existing movement controls.

1 Like

[quote] It’d be nice if we could toggle auto state changing. [/quote]I agree with this. It would solve the problem above by switching it on when the character enters the water and off when they exit.

1 Like

That was my intention, yeah. It would be strange as a property of Humanoids, in my opinion.
Maybe a second argument like Humanoid:ChangeState(humanoidState, persistantBool)

Upon entering the water, you would set it as swimming, true and when they leave the water, you would just set it to whatever happens after swimming and false.

it was a suggestion relating to yours, not a solution so chill out.

Have you tried connecting to the humanoid’s StateChanged event instead of RenderStepped?

Sorry if my reply came off as not chill osyris & biscuit I saw a large flaw and so posted that d:

[strike]Tomarty I’m about to try your suggestion and will edit this post with results[/strike]

[code]p = game.Players.LocalPlayer
hum = p.Character.Humanoid

hum.StateChanged:connect(function(state)
print(state)
hum:ChangeState’Swimming’
end)
[/code]

Same problem ):

If I’m correct, the swim state is designed to work with the buoyancy physics of water.

Just out of curiosity, why are you not using smooth terrain water?

I provide my own buoyancy with a bodyposition.

Smooth terrain water is not suitable for an ocean. FPS lag, join times, and complaints go through the roof.

1 Like

[quote] I provide my own buoyancy with a bodyposition.

Smooth terrain water is not suitable for an ocean. FPS lag, join times, and complaints go through the roof. [/quote]

This is why we need waterparts.
Enum.Material.Water exists, but it doesn’t show up last I checked

2 Likes

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)



9 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.