Disabling Swimming in Water

Hello, I am attempting to create a custom script that allows people to walk underwater if they are in an enclosed space. However, the script does not disable a player’s swimming ability and I don’t know why. Please help.

local players = game:GetService("Players")

local player = players:WaitForChild("gogomangothacked2341")
local character = player.Character or player.CharacterAdded:Wait()
local humanoid: Humanoid = character.Humanoid
humanoid:SetStateEnabled(Enum.HumanoidStateType.Swimming, false)

Is this script ran in a localscript or server script?

1 Like

I run it in both local and server because I noticed that the settings are different between server and client

i checked a few posts and one of them say run it on both local and server, but you already are, a scriptinghelpers post says that they fixed it by putting the localscript into startergui. maybe try checking if the player is swimming and then disabling it if the others dont work.

actually i tried it myself and it worked for me? and its in a localscript in startercharacterscripts

2 Likes

Putting it in StarterCharacterScripts worked. Thank you very much!

Unsure why it didn’t work in the command line but oh well.

1 Like

So it stopped working when i tried to put it into a runservice.RenderStepped event. Here is my code so far:

-- Services

local players = game:GetService("Players")
local runService = game:GetService("RunService")

-- Variables

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local head = character:WaitForChild("Head")

-- Scripts

local function checkIfWalk()
	local min = head.Position - (.5 * head.Size)
	local max = head.Position + (.5 * head.Size)	
	local region = Region3.new(min,max):ExpandToGrid(4)

	local material = workspace.Terrain:ReadVoxels(region,4)[1][1][1]

	if material == Enum.Material.Water then
		local castParams = RaycastParams.new()
		castParams.IgnoreWater = true

		local castResult = workspace:Raycast(head.Position, head.Position + Vector3.new(0, 25000, 0), castParams)

		if not castResult then return end
		if not castResult.Instance then return end

		return true
	end
end

runService.RenderStepped:Connect(function(deltaTime)
	if checkIfWalk() then
		humanoid:SetStateEnabled(Enum.HumanoidStateType.Swimming, false)
		for _, limb: BasePart in pairs(character:GetChildren()) do
			if not limb:IsA("BasePart") then continue end

			limb.CustomPhysicalProperties = PhysicalProperties.new(10, 0.3, 0.5)
		end
	else
		humanoid:SetStateEnabled(Enum.HumanoidStateType.Swimming, true)
		for _, limb: BasePart in pairs(character:GetChildren()) do
			if not limb:IsA("BasePart") then continue end

			limb.CustomPhysicalProperties = PhysicalProperties.new(0.7, 0.3, 0.5)
		end
	end
end)

It changes the physical properties just fine and when I print humanoid:GetStateEnabled(Enum.HumanoidStateType.Swimming), it prints false.

So do you idea why it’s doing this?

1 Like

I easily fixed this by seating the character then unseating it