Custom Swimming doesn't allow player to go upwards?

So i’ve been trying to make a part that when you go into (can collide off) will allow you to swim.
I have made a script that does so but a bug i’ve encounted is that when you start swimming you can’t go upwards. It just seems like you are stuck.

Here is a picture:

2 Likes

May you provide the script and the video?

1 Like

Fast reply!! Sure i’ll provide a script. Sorry if it’s messy i’ve tested many things.

1 Like
local Sea = workspace.WaterPart1

local RS = game:GetService("RunService")
local Character = script.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local ContextActionService = game:GetService("ContextActionService")
local ForceValue = (workspace.Gravity + (1500)) --1500

local InWater = false
local KeysHeld = {}
local Swim
local Swimming
local UpwardForce

function Stop()
	coroutine.resume(coroutine.create(function()
		Humanoid.Parent.HumanoidRootPart.Anchored = true
		wait(0.2)
		Humanoid.Parent.HumanoidRootPart.Anchored = false
	end))
end
coroutine.resume(coroutine.create(function()
	UpwardForce = Instance.new("BodyVelocity")
	while wait() do
		if not UpwardForce then
			UpwardForce = Instance.new("BodyVelocity")
			UpwardForce.Parent = Root
		end
		if InWater and not Swimming then
			UpwardForce.Velocity = Vector3.new(0,4,0)
		elseif InWater and Swimming then
			UpwardForce.Velocity = Vector3.new(0,0,0)
		else
			UpwardForce:Destroy()
			UpwardForce = nil
		end
	end
end))

game:GetService("UserInputService").InputBegan:connect(function(input)
	--input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D
	if InWater then
		if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then
			KeysHeld[#KeysHeld+1] = input.KeyCode
		end
		print('Swimming')
		Humanoid.Parent.HumanoidRootPart.Anchored = false
		Swimming = true
		if Swim and InWater and #KeysHeld >= 0 then
			Swim.Force = Vector3.new(0,ForceValue,0)
		elseif Swim and InWater and #KeysHeld == 0 then
			Swim.Force = Vector3.new(0,0,0)
		elseif Swim and not InWater then
			Swim.Force = Vector3.new(0,0,0)
		elseif Swim then
			Swim.Force = Vector3.new(0,0,0)
		end
	end
end)
game:GetService("UserInputService").InputEnded:connect(function(input)
	local TablePos = table.find(KeysHeld, input.KeyCode)
	table.remove(KeysHeld, TablePos)
	if InWater and Humanoid.MoveDirection.Magnitude <= 0 then
		print('Not Swimming')
		Swimming = false
	end
end)

Sea.Touched:Connect(function(Hit)
	if Hit and Hit.Name == 'HumanoidRootPart' and Hit.Parent:FindFirstChild('Humanoid') then
		if not Swim then
			Swim = Instance.new("BodyForce")
			Swim.Parent = Root
		end
		Swim.Force = Vector3.new(0,ForceValue,0) -- 2000 is optimum
		--Humanoid.JumpPower = 0
		Humanoid:SetStateEnabled("GettingUp", false)
		Humanoid:ChangeState("Swimming")
		InWater = true
	end
end)

Sea.TouchEnded:Connect(function(Hit)
	if Hit and Hit.Name == 'HumanoidRootPart' and Hit.Parent:FindFirstChild('Humanoid') and Swim then
		InWater = false
		Swim:Destroy()
		Swim = nil
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
	end
end)

Here is a quick video:
https://streamable.com/epprr9