Humanoid SetStateEnabled Not Working

I was trying to make the player swim when touching this part with my own script but it wasn’t working so I switched to making it die to see if it worked AT ALL. It doesn’t.

I could only find these:

Why is it not working? This is my code:

local deb = false

for i, v in pairs(workspace:GetDescendants()) do
	if v:IsA("BasePart") or v:IsA("MeshPart") then
		v.Touched:Connect(function(hit)
			if hit.Parent.Name ~= "Swimmable" then return end
			print("hit")
			if deb == true then return end
			deb = true
			script.Parent:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Swimming, true)
			print("now swimming")
		end)
		
		v.TouchEnded:Connect(function(hit)
			if hit.Parent.Name ~= "Swimmable" then return end
			print("hit")
			if deb == false then return end
			deb = false
			script.Parent:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Swimming, false)
			print("not swimming")
		end)
	end
end

Edit: All prints do work so it thinks its setting the state but it isn’t actually doing anything.

I put it inside of StarterCharacterScripts as a Local Script

Hello, in your script, you are simply telling the Humanoid if they can enter the Swimming state or not, but you aren’t really changing the state itself.

Source: https://create.roblox.com/docs/reference/engine/classes/Humanoid#SetStateEnabled

You can just modify your script to use Humanoid:ChangeState() instead.

I copied your script and simply replaced Humanoid:SetStateEnabled() with Humanoid:ChangeState()

Here’s the working code:

local deb = false

for i, v in pairs(workspace:GetDescendants()) do
	if v:IsA("BasePart") or v:IsA("MeshPart") then
		v.Touched:Connect(function(hit)
			if hit.Parent.Name ~= "Swimmable" then return end
			print("hit")
			if deb == true then return end
			deb = true
			script.Parent:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.Swimming, true)
			print("now swimming")
		end)

		v.TouchEnded:Connect(function(hit)
			if hit.Parent.Name ~= "Swimmable" then return end
			print("hit")
			if deb == false then return end
			deb = false
			script.Parent:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.Swimming, false)
			print("not swimming")
		end)
	end
end

Hey, this actually works! Although only half of it. When I enter it lets me swim but if i stay in it stops. I thought this was due to the second half with “v.TouchEnded” but when I deleted that the same issue happened:
https://medal.tv /games/roblox-studio/clips/l8SSu3yyWfyxJTN9C

Edit: You will need to get rid of the space between tv and /games

This is because the other states can still be entered. You can disable other states besides swimming or depending on what your game needs so it doesnt try to fight each other. However you might still encounter weird physics issues after the said fix above as you dont have the “Water Physics” like buoyancy and etc. Compared to the actual water terrain.

I have marked you as solved but incase anyone else looks here, I have this as the local script:

local deb = false
local disable = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))

for i, v in pairs(workspace:GetDescendants()) do
	if v:IsA("BasePart") or v:IsA("MeshPart") then
		v.Touched:Connect(function(hit)
			if hit.Parent.Name ~= "Swimmable" then return end
			print("hit")
			if deb == true then return end
			deb = true
			local char = script.Parent
			disable.everything(char)
			script.Parent:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.Swimming, true)
			print("now swimming")
		end)

		v.TouchEnded:Connect(function(hit)
			if hit.Parent.Name ~= "Swimmable" then return end
			print("hit")
			if deb == false then return end
			deb = false
			local char = script.Parent
			disable.nothing(char)
			script.Parent:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.Swimming, false)
			print("not swimming")
		end)
	end
end

and then this as a module script:

local disable = {}

function disable.everything(char)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Seated, false)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Landed, false)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.PlatformStanding, false)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Flying, false)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Running, false)
end

function disable.nothing(char)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Seated, true)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Landed, true)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Ragdoll, true)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.PlatformStanding, true)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Flying, true)
	char:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Running, true)
end

return disable

1 Like

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