BodyVelocity and Humanoid.MoveDirection

Hello! I recently decided to make a swim script (Like in Crazyblox’s Flood Escape 2), where if you touch a part named “Acid” you start to swim. I read some forum posts and they said that the best way to do this is BodyMovers and ChangeState.

Issue is, when the player moves up (aka presses Spacebar), or moves down (presses LeftShift) it works just fine. But when I try to make the player swim Horizontally, the player will either not move at all, or they will move a small bit and it will be very sloppy and then they will stop and “freeze?”. I might just be making a dumb mistake. I use MoveDirection to see where they are moving.

I’ve tried looking at the devforums but the only things I found were topics on how to achieve this.

https://gyazo.com/4d9cd7fa1416f465212ec1ef231f0611


local player = game.Players.LocalPlayer;

repeat
	wait()
until
player.Character

local character = player.Character;
local humanoid = character.Humanoid;
local root = character:WaitForChild("HumanoidRootPart");
local head = character:WaitForChild("Head");
local swimming = false;
local bp = nil
local userInputService = game:GetService("UserInputService");
local velocity = nil;

head.Touched:Connect(function(hit)
	print("Touched")
	if hit then
		if hit.Parent then
			if hit.Name == "Acid" then
				print("Can swim")
				swimming = true
				humanoid:ChangeState(Enum.HumanoidStateType.Freefall)
				if velocity == nil then
					velocity = Instance.new("BodyVelocity", root)
					velocity.Velocity = humanoid.MoveDirection
					while swimming do
						userInputService.InputBegan:Connect(function(input)
							if velocity ~= nil then							
								if input.KeyCode == Enum.KeyCode.Space then
									if velocity ~= nil then
										velocity.Velocity = Vector3.new(humanoid.MoveDirection.X * 10, 10, humanoid.MoveDirection.Z * 10)
									end			
								elseif input.KeyCode == Enum.KeyCode.LeftShift then
									if velocity ~= nil then
										velocity.Velocity = Vector3.new(humanoid.MoveDirection.X * 10, -10, humanoid.MoveDirection.Z * 10)
									end				
								else
									if velocity ~= nil then
										velocity.Velocity = Vector3.new(humanoid.MoveDirection.X * 10, 0, humanoid.MoveDirection.Z * 10)
									end					
								end	
							end				
						end)
						userInputService.InputEnded:Connect(function(input)
							if velocity ~= nil then
								velocity.Velocity = Vector3.new(0,0,0)
							end
						end)
						wait()
					end
				end
				wait()
			end
		end
	end
end)

head.TouchEnded:Connect(function(hit)
	print("Touch ended")
	if hit then
		if hit.Parent then
			if hit.Name == "Acid" then
				swimming = false
				humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
				if velocity ~= nil then
					velocity:Destroy()
					velocity = nil
				end
			end
		end
	end	
end)

Any help is appreciated!

2 Likes

Im very sorry that this is a late reply but instead of using move direction, use Vector3.new(0,1,0) * math.huge for the max force and use vector3.new(0,50,0) for going up and vector3.new(0,-50,0) for going down