Why does this fire multiple times

I’m trying to change the humanoid state but it unsets itself and fires multiple times

LocalScript:

if script.Parent.Humanoid:GetState() ~= Enum.HumanoidStateType.Swimming then
			print("e")
			script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Swimming)
		end

The humanoid’s state is something that’s constantly changing so you’ll need to check its current value within a loop

Pretty sure that state would also depend on if they are in water. Considering it kicks in as soon as you are in water, it is clearly part of the stack checking for that in real-time.

So If you’re not in water swimming ~= yes, it will fire constantly.

1 Like

the code is in a runservice loop

1 Like

By the way, if you want to force the Humanoid’s state to be Swimming you can use this method instead to do so:

local humanoid = script.Parent.Humanoid

for _, humanoidStateType in Enum.HumanoidStateType:GetEnumItems() do
	if humanoidStateType == Enum.HumanoidStateType.Swimming then continue end
	if humanoidStateType == Enum.HumanoidStateType.None then continue end -- Else you'll get the error: Invalid state passed to SetStateEnabled.

	humanoid:SetStateEnabled(humanoidStateType, false)
end

humanoid:ChangeState(Enum.HumanoidStateType.Swimming)

Although you will need to reenable them once you’d like to give the other states back. To do so all you need to do is to run this code again but change the second argument of SetStateEnabled from false to true

Found this post … closet thing I could find.
[Swimable part / Fake water]